note 36765 modified in function.array-pop by vrana

From: Date: Tue, 17 Aug 2004 14:48:07 +0000
Subject: note 36765 modified in function.array-pop by vrana
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-79969@lists.php.net to get a copy of this message
Be aware that using array_pop on an associative array that uses a numeric string as a key changes the key: <?php $stack= array("12" => "green", "54" => "brown", "672" => "blue"); print_r($stack); $fruit = array_pop($stack); print_r($stack); ?> Results of execution: Array ( [12] => green [54] => brown [672] => blue ) Array ( [0] => green [1] => brown ) However, if there is a non-numeric character in the key, the key will be maintained: <?php $stack= array("g1" => "green", "b1" => "brown", "b2" => "blue"); print_r($stack); $fruit = array_pop($stack); print_r($stack); ?> Results of execution: Array ( [g1] => green [b1] => brown [b2] => blue ) Array ( [g1] => green [b1] => brown ) --was-- Be aware that using array_pop on an associative array that uses a numeric string as a key changes the key: $stack= array("12" => "green", "54" => "brown", "672" => "blue"); print_r($stack); $fruit = array_pop($stack); print_r($stack); Results of execution: Array ( [12] => green [54] => brown [672] => blue ) Array ( [0] => green [1] => brown ) However, if there is a non-numeric character in the key, the key will be maintained: $stack= array("g1" => "green", "b1" => "brown", "b2" => "blue"); print_r($stack); $fruit = array_pop($stack); print_r($stack); Results of execution: Array ( [g1] => green [b1] => brown [b2] => blue ) Array ( [g1] => green [b1] => brown ) http://php.net/manual/en/function.array-pop.php

« previous php.notes (#79969) next »