Description
int
array_push ( array array, mixed var [, mixed ...])
array_push() treats
array as a stack, and pushes the passed
variables onto the end of array. The
length of array increases by the number of
variables pushed. Has the same effect as:
repeated for each
var.
Returns the new number of elements in the array.
Example 1. array_push() example $stack = array ("orange", "banana");
array_push ($stack, "apple", "raspberry"); |
This example would result in $stack having
the following elements:
Array
(
[0] => orange
[1] => banana
[2] => apple
[3] => raspberry
) |
|
See also array_pop(),
array_shift(), and
array_unshift().
User Contributed Notes array_push |
 |
misc.anders@feder.dk
03-Feb-2001 03:43 |
|
The $array[] = $var statement differs from array_push($array, $var) in that
it can operate on uninitialized variables as well. array_push only works
with initialized arrays.
|
|
nemo@altern.org
26-Mar-2001 04:12 |
|
la solution $array[$key]=$value; est une bonne solution pour ajouter une
paire à un tableau, mais elle pose problème lorsqu'on a des $key
identiques. Array_push est donc ici une bonne alternative.
|
|
bart@framers.nl
27-Sep-2001 10:16 |
|
Array_push also works fine with multidimensional arrays. Just make sure the
element is defined as an array first.
$array["element"][$element]["element"] = array();
array_push ($array["element"][$element]["element"],
"banana");
|
|
linuxbaby@yahoo.com
28-Jan-2002 10:13 |
|
Interesting! Maybe $a[]=$b is NOT the same as array_push($a, $b) - when you
want the VALUE of an array to also be an array.
$a = array();
$b = array("one","example","array");
$c = array("another","silly","test");
$a[] = $b; /* ERROR */
$a[] = $c; /* ERROR */
but...
array_push($a, $b); /* WORKS */
array_push($a, $c); /* WORKS */
|
|
carl_steinhilber@mentor.com
29-Jan-2002 08:56 |
|
array_push() doesn't seem to allow you to push a key=>value pair onto
the array.
i.e.:
$myarray = array ();
:
array_push($myarray,"mykey"=>"myvalue");
produces a parsing error.
|
|
Anonymous@poster.com
30-Jan-2002 10:06 |
|
Regarding:
Carl wrote that: "array_push() doesn't seem to allow you to push a
key=>value pair onto the array."
The correct syntax for this would be:
$array['newkey']=$newvalue;
You should not need the array_push function, but if so the example should
read:
array_push($myarray,array("mykey"=>"myvalue"));
This will not return a syntax error.
|
|
Static
04-Feb-2002 06:19 |
|
I noticed that when building a large array, array_push($array, $value)
didn't seem to be quite as fast as $array[] = $value. Using the latter
would therefore be a useul optimisation if you can guarantee unique keys
(e.g. reading from a DB table with an id field).
Static.
|
|
|
12-Feb-2002 11:02 |
|
Anonymous said:
array_push($myarray,array("mykey"=>"myvalue"));
This will not return a syntax error.
But this is totally incorrect. It's equivalent to:
$myarray[count($myarray)] =
array("mykey"=>"myvalue"));
|
|
gigabite@gmx.de
28-Feb-2002 01:37 |
|
---------------------------------------
Anonymous said:
array_push($myarray,array("mykey"=>"myvalue"));
This will not return a syntax error.
But this is totally incorrect. It's equivalent to:
$myarray[count($myarray)] =
array("mykey"=>"myvalue"));
---------------------------------------
yes, but as you can see in the function definition,
array_push($arr, $newval)
is the same as
$arr[] = $newval
and therefor
array_push($arr, array("key" => "val")
is the same as $arr[] = array("key" => "val")
here's a function which will perform as you want to:
---------------------------------------
function array_push_assoc(&$array, $key, $val)
{
if (!is_array($array)) {
return 0;
}
$args = func_get_args();
for($i = 1; $i < func_num_args(); $i + 2) {
if (is_string($args[$i]) && trim($args[$i]) !=
"") {
$array[$args[$i]] = $args[$i + 1];
}
}
return sizeof($array);
}
---------------------------------------
|
|
alexg@syspro.co.za
12-Mar-2002 03:02 |
|
Here`s a shorter way of adding "key=>value" values
$myarray = array( "mykey"=>"myvalue" );
$myarray = array_merge( $myarray,array( "mykey2" =>
"myvalue2" ));
|
|
marc@spoorendonk.com
12-Apr-2002 09:06 |
|
But that doesn't work for integer keys, because they will be renumbered by
the array_merge(). (Which is the problem that brought me to this exact page
;))
|
|
daniel@nomail.com
14-Apr-2002 01:17 |
|
If you want to use integer keys simply treat it as an 'ordinary' array.
$user_arr = array();
$user_id = 4;
$user_name = 'John';
$user_arr[$user_id] = $user_name;
$user_id = 346;
$user_name = 'Steve';
$user_arr[$user_id] = $user_name;
$user_id = 652;
$user_name = 'Maria';
$user_arr[$user_id] = $user_name;
All slots in between the three user IDs will not be allocated and
sizeof($user_arr) will return 3 and not 653.
|
|
 |