Monday 17 September 2012

php array append

PHP: append an element to the end of the array


NoteIf you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.
---- http://php.net/manual/en/function.array-push.php

Example:

$elements = array(1,2,3,4,5,6);

foreach ($elements as $elem)
    $newArray[] = $elem;

print_r($newArray);
//sample output: Array ( [0] => 1[1] => 2[2] => 3[3] => 4...)

No comments:

Post a Comment