When using php code to make web pages, simple operations on arrays are often used, such as appending arrays, merging arrays, etc. In this article, I briefly share some methods of merging, appending or connecting multiple arrays in php code.
1. Add array elements
One-dimensional array:
$array = array('a','b','c');
$array[] = "d";
Two-dimensional array:
$array = array('0'=>array('0'=>'a'),'1'=>array('0'=>'b'));
$array[3][]=array('0'=>'c');
$array[3][0]='c';
The array_push() function can also add one or more elements to the end of the array, which is equivalent to calling $array[] = $xxxx multiple times. Added elements are always numeric key names.
array_push($array,"d","e");
If you use array_push to add only one element to the array, it is better to use the [] method, and use square brackets directly without calling the function.
To add a new element to the array $array, you can directly add square brackets after the array, the key name in the square brackets, and then the equal sign assigns it.
For arrays, square brackets are also added. Each time a set of square brackets is added, it goes to the next dimension. For example, $array[3] represents an array whose key name is 3 in the two-dimensional array $array, and $array[3][0] represents It is mostly the value of the element whose key name is 0 in the one-dimensional array whose key name is 3 in the array.
2, delete array elements
If you need to delete an element in an array, you can generally use the unset() or array_splice() methods in PHP code.
$array = array('a','b','c');
unset($array[1]);
array_splice($array, 1, 2);
Unset delete array element method in php will not change other keys, if you want to rearrange and sort other numeric key names, you need to use the array_values() method
array_values($array);
The array_values() method will return an array of all key values in the specified array, without saving the key names, and start again from 0.
Then, array_splice() will automatically re-index the key name of the array, but the associative array does not work. The first parameter is the array, the second parameter is the starting position of the deleted element, and the third parameter is the deletion number of .
To delete multiple elements, you can choose the array_diff and array_diff_key methods
array_diff() method
$array = array(0 => "ww", 1 => "tt", 2 => "vv");
$array = array_diff($array, ["ww", "vv"]);
$array = array_diff_key($array, [0,2]);
You can use array_diff when you know the value of the array element to be deleted, and use array_diff_key when you know the key name of the array element to be deleted. Write the key value or key name to be deleted in square brackets.
At some point, in a multidimensional array, we need to delete the specified in the multidimensional array according to the name of the key?
code show as below:
function keyscarray($shuzu, $szkey){
if(!array_key_exists($szkey, $arr)){
return $shuzu;
}
$szkeys = array_keys($shuzu);
$suoying = array_search($szkey, $szkeys);
if($index !== FALSE){
array_splice($shuzu, $suoying, 1);
}
return $shuzu;
}
Then directly keyscarray(array, key name); you can delete most of the values specified in the array.
3. Merge arrays
To merge arrays in php, the array_merge and array_merge_recursive functions are generally used.
$ar1 = array("1","2","3");
$ar2 = array("a","b","c");
array_merge($ar1,$ar2);
The array_merge function will merge the arrays together and return the array. The obtained array is forcibly added from left to right in the order in which the array appears. Multiple additions can be made at one time. If the array has the same literal key name, the value after the key name will overwrite the previous value. If the array contains numbers The key name of the type, the new value will not overwrite the original value in the previous array, but will be appended,
Arrays with only one numeric index, then the appended key names will be re-indexed
$ar1 = array("a" => 100, "b" => 85);
$ar2 = array("c" => 78, "a" => 45);
$classScores = array_merge_recursive($ar1, $ar2);
The array_merge_recursive function is the same as array_merge in that they can combine multiple arrays to form a new array. But when a key name in the appended array already exists, they will be handled differently.
array_merge will Tidy up the code,Overwrite the previous key values in the array code, and array_merge_recursive will merge two values with the same key name together, forming a new array with the original key as the array name.
To merge arrays in php, there is also an array_combine function to get a new array but it is composed of a set of key names and corresponding values
$ar1 = array("1","2","3");
$ar2 = array("a","b","c");
array_combine($ar1,$ar2);
In array_combine, ar1 is the key name of the new array, and ar2 is the corresponding key value in the array.
The above are several common ways to deal with arrays in php code.