PHP: Add new Element to existing Array
Question by Compi | 2016-02-08 at 17:36
It is possible to create a new array with $arr = array(1, 2, 3) in PHP. After that, the array is containing the three elements 1, 2 and 3.
However, is it also possible to add some more elements to an array that is already existing? I am searching for a way with which I can insert some new items at the end of an array after it has already been created or even processed.
Related Topics
PHP: Remove all empty elements from string array
Tip | 0 Comments
JavaScript: Create and Use Arrays
Info | 0 Comments
Send Form Input as an Array to PHP Script
Tip | 0 Comments
Lazarus: Load File as Byte Array and save Byte Array as File
Tutorial | 0 Comments
PHP: Write array to individual Variables
Tutorial | 0 Comments
jQuery: Does an Element exist?
Tip | 1 Comment
PHP: Fill Array with Sequence of Numbers or Characters
Tutorial | 0 Comments
Important Note
Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.
Participate
Ask your own question or write your own article on askingbox.com. That’s how it’s done.
Yes, that is possible.
For me, the easiest way is the following:
With that code, you are inserting the new elements at the end of the array. In the example, our array afterwards contains 5 elements, the numbers 1 to 5.
2016-02-09 at 11:47
Es geht auch mit der Funktion array_push(). Dieser Funktion kannst du den Array und die neuen Elemente übergeben:
Wie du siehst kannst du mit array_push() auch mehrere Elemente gleichzeitig hinzufügen oder sogar einen kompletten weiteren Array. In diesem Beispiel enthält $arr am Ende die Elemente 1 bis 10.
Last update on 2020-09-03 | Created on 2016-02-09