note 19468 added to function.array-push
| From: | gigabite at gmx dot de | Date: | Thu, 28 Feb 2002 12:37:11 +0000 |
| Subject: | note 19468 added to function.array-push | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-27161@lists.php.net to get a copy of this message | ||
---------------------------------------
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);
}
---------------------------------------
--
http://www.php.net/manual/en/function.array-push.php
http://master.php.net/manage/user-notes.php?action=edit+19468
http://master.php.net/manage/user-notes.php?action=delete+19468
http://master.php.net/manage/user-notes.php?action=reject+19468