note 97498 deleted from function.array-pop by danbrown

From: Date: Fri, 23 Apr 2010 12:33:59 +0000
Subject: note 97498 deleted from function.array-pop by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-168554@lists.php.net to get a copy of this message
Note Submitter: adaelxp at gmail dot com ---- <?php // Retrieve the item by index and recalculate array index function array_pop_index(&$array, $index){ if( ! isset($array[$index]) ) return null; $temp = $array[$index]; unset($array[$index]); $array = array_values($array); return $temp; } // Retrieve the item by key and unset it (useful for associative arrays) function array_kpop(&$array, $key){ if(!isset($array[$key])) return null; $temp = $array[$key]; unset($array[$key]); return $temp; } // Use: $arr = array('bananas', 'lemons', 'oranges'); $str = array_pop_index($arr, 1); echo $str . "\n"; // output "lemons" print_r($arr); /* Output: Array ( [0] => bananas [1] => oranges ) */ $karr = array('one' => 'bananas', 'two' => 'lemons', 'three' => 'oranges'); $str = array_kpop($karr, 'one'); echo $str . "\n"; // output "bananas" print_r($karr); /* Output: Array ( [one] => bananas [three] => oranges ) */ ?>

« previous php.notes (#168554) next »