HOWTO · PHP
在 PHP 中將元素新增到關聯陣列
本教程演示如何在 PHP 中將元素新增到關聯陣列。
本頁內容
PHP 有不同的方法將專案新增到關聯陣列中。
如果我們想將專案新增到陣列的開頭,我們可以使用內建函式,如 array_merge()。
我們需要動態地在關聯陣列的特定鍵之前新增元素。
在 PHP 中將元素新增到關聯陣列的末尾
我們可以通過新增鍵和值或將新的鍵值連線到陣列來將元素新增到關聯陣列的末尾。
<?php
//First Method
$demo_array = array('Jack' => '10');
$demo_array['Michelle'] = '11'; // adding elements by pushing method
$demo_array['Shawn'] = '12';
echo "By Simple Method: <br>";
print_r($demo_array);
echo "<br>";
echo "Replacing the value: <br>";
$demo_array['Jack'] = '13'; // replaces the value at Jack
print_r($demo_array);
echo "<br>";
//Second method
//$demo_array += [$key => $value];
$demo_array += ['John' => '14'];
echo "By Concating Method: <br>";
print_r($demo_array);
?>
上面的程式碼嘗試通過兩種方法將元素新增到陣列的末尾。
輸出:
By Simple Method:
Array ( [Jack] => 10 [Michelle] => 11 [Shawn] => 12 )
Replacing the value:
Array ( [Jack] => 13 [Michelle] => 11 [Shawn] => 12 )
By Concating Method:
Array ( [Jack] => 13 [Michelle] => 11 [Shawn] => 12 [John] => 14 )
在 PHP 中使用 array_merge() 函式在關聯陣列的開頭新增元素
要在關聯的開頭新增元素,我們可以使用 array_merge() 函式的陣列聯合。
<?php
$demo_array = array('Senior Developer' => 'Jack', 'Junior Developer' => 'Michelle', 'Intern' => 'John');
echo "The original array : ";
print_r($demo_array);
$temp_array = array('Project Manager' => 'Shawn'); //As per hierarchy Project manager should be at number 1.
// Using array union
$union_array = $temp_array + $demo_array;
echo "<br> The new associative array using union : ";
print_r($union_array);
// Using array_merge() function
$merge_array = array_merge($temp_array, $demo_array);
echo "<br> The new associative array using Array_merge : ";
print_r($merge_array);
?>
上面的程式碼使用 union_array 方法和 array_merge() 在陣列的開頭新增元素。
輸出:
The original array : Array ( [Senior Developer] => Jack [Junior Developer] => Michelle [Intern] => John )
The new associative array using union : Array ( [Project Manager] => Shawn [Senior Developer] => Jack [Junior Developer] => Michelle [Intern] => John )
The new associative array using Array_merge : Array ( [Project Manager] => Shawn [Senior Developer] => Jack [Junior Developer] => Michelle [Intern] => John )
在 PHP 中使用 AddBetween 函式的關聯陣列之間新增元素
PHP 沒有在給定陣列之間新增元素的內建函式。但是我們可以建立一個在給定鍵之前新增元素的函式。
<?php
function AddBetween( $original_array, $before_key, $new_key, $new_value ) {
$added_array = array();
$added_key = false;
foreach( $original_array as $key => $value ) {
if( !$added_key && $key === $before_key ) {
$added_array[ $new_key ] = $new_value;
$added_key = true;
}
$added_array[ $key ] = $value;
}
return $added_array;
}
$demo_array = array('Project Manager' => 'Shawn', 'Senior Developer' => 'Jack', 'Intern' => 'John');
echo "The Original Array is: <br>";
print_r($demo_array);
echo "<br>";
//Add 'Junior Developer' => 'Michelle' before intern as per hierarchy.
$added_array = AddBetween( $demo_array, 'Intern', 'Junior Developer', 'Michelle' );
echo "The Array With Added Element is: <br>";
print_r($added_array);
?>
AddBetween 函式嘗試在給定鍵之前新增一個元素。
輸出:
The Original Array is:
Array ( [Project Manager] => Shawn [Senior Developer] => Jack [Intern] => John )
The Array With Added Element is:
Array ( [Project Manager] => Shawn [Senior Developer] => Jack [Junior Developer] => Michelle [Intern] => John )
根據層次結構,在 intern 之前新增了 Junior Developer。