note 19468 deleted from function.array-push by didou
| From: | didou@php.net | Date: | Tue, 29 Jul 2003 11:59:18 +0000 |
| Subject: | note 19468 deleted from function.array-push by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-52915@lists.php.net to get a copy of this message | ||
Note Submitter: gigabite@gmx.de
----
---------------------------------------
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);
}
---------------------------------------