note 102215 added to function.array-slice

From: Date: Thu, 03 Feb 2011 03:23:05 +0000
Subject: note 102215 added to function.array-slice
Groups: php.notes 
Request: Send a blank email to php-notes+get-176610@lists.php.net to get a copy of this message
<?php /** * Reorders an array by keys according to a list of values. * @param array $array the array to reorder. Passed by reference * @param array $list the list to reorder by * @param boolean $keepRest if set to FALSE, anything not in the $list array will be removed. * @param boolean $prepend if set to TRUE, will prepend the remaining values instead of appending them * @author xananax AT yelostudio DOT com */ function array_reorder(array &$array,array $list,$keepRest=TRUE,$prepend=FALSE,$preserveKeys=TRUE){ $temp = array(); foreach($list as $i){ if(isset($array[$i])){ $tempValue = array_slice( $array, array_search($i,array_keys($array)), 1, $preserveKeys ); $temp[$i] = array_shift($tempValue); unset($array[$i]); } } $array = $keepRest ? ($prepend? $array+$temp :$temp+$array ) : $temp; } /** exemple ** / $a = array( 'a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd', 'e' => 'e' ); $order = array('c','b','a'); array_reorder($a,$order,TRUE); echo '<pre>'; print_r($a); echo '</pre>'; /** exemple end **/ ?> ---- Server IP: 69.147.83.197 Probable Submitter: 78.108.169.9 (proxied: 213.204.68.119) ---- Manual Page -- http://www.php.net/manual/en/function.array-slice.php Edit -- https://master.php.net/note/edit/102215 Del: integrated -- https://master.php.net/note/delete/102215/integrated Del: useless -- https://master.php.net/note/delete/102215/useless Del: bad code -- https://master.php.net/note/delete/102215/bad+code Del: spam -- https://master.php.net/note/delete/102215/spam Del: non-english -- https://master.php.net/note/delete/102215/non-english Del: in docs -- https://master.php.net/note/delete/102215/in+docs Del: other reasons-- https://master.php.net/note/delete/102215 Reject -- https://master.php.net/note/reject/102215 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#176610) next »