A summary of methods for adding elements to php arrays, a summary of php array elements
This article summarizes the method of adding elements to PHP arrays in more detail. Share it with everyone for your reference. The specific analysis is as follows:
If we are adding array elements to a one-dimensional array, we can use ArrayListay_push. Of course, in addition to this method, we have more direct methods. Here we will sort it out for you.
Add elements to one-dimensional array
Copy code The code is as follows:
$ArrayList = ArrayListay();
Array_push($ArrayList, el1, el2 ... eln);
But there is actually a more direct and convenient way, the code is as follows:
Copy code The code is as follows:
$ArrayList = ArrayListay();
$ArrayList[] = el1;
$ArrayList[] = el2;
...
$ArrayList[] = eln;
The results obtained by the two methods are the same.
Add one or more elements, the code is as follows:
Copy code The code is as follows:
$ArrayList1 = ArrayListay("a", "b");
Array_push($ArrayList1, "c", "d");
Print_r($ArrayList1);
?>
The running results are as follows:
Copy code The code is as follows:
ArrayListay
(
[0] => a
[1] => b
[2] => c
[3] => d
)
Note: ArrayListay_push() will issue a warning if the first parameter is not an array.
Insert element at the beginning of the array
1. ArrayListay_unshift, usage method, the code is as follows:
Copy code The code is as follows:
$array_Qlist = ArrayListay("military-civilian joint defense", "banana");
Array_unshift($array_Qlist, "Watermelon", "Bangkezhijia");
print_r($array_Qlist);
?>
The output is as follows:
Copy code The code is as follows:
ArrayListay
(
[0] => Watermelon
[1] => Bangke’s Home
[2] => Military-civilian joint defense
[3] => banana
)
It is not feasible to use ArrayListay_push or ArrayListay_unshift to add associated elements to the array. So how do we add it? Use the ArrayListay_merge method to achieve a function similar to ArrayListay_unshift that adds elements at the beginning. The code is as follows:
Copy code The code is as follows:
$array_Qlist = ArrayListay('a', 'B');
$array_Qlist = Array_merge(ArrayListay('front' => 'Hello www.jb51.net'), $array_Qlist);
/*
ArrayListay
(
[front] => Hello www.jb51.net
[0] => a
[1] => b
)
*/
?>
2. + operator, the code is as follows:
Copy code The code is as follows:
$array_Qlist = Array('a', 'B');
$array_Qlist = Array('front' => 'Hello little www.jb51.net') + $array_Qlist;
?>
The output result is the same as using the ArrayListay_merge method.
3. Add an associative array element at the end of the element, the code is as follows:
Copy code The code is as follows:
$array_Qlist = ArrayListay('a', 'B');
$array_Qlist['front'] = 'Hello www.jb51.net';
/*
Output
ArrayListay
(
[0] => a
[1] => b
[front] => Hellowww.jb51.net
)
*/
?>
As for some friends who say that the performance of Array_push adding elements is worse than that of direct $a[]='a', everything else is the same. If there is only one value, there is no comparison.
I hope this article will be helpful to everyone’s PHP programming design.
http://www.bkjia.com/PHPjc/930489.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/930489.htmlTechArticleSummary of methods for adding elements to php arrays, summary of php array elements This article summarizes the methods of adding elements to php arrays in more detail. Share it with everyone for your reference. The specific analysis is as follows: If I...