note 53574 deleted from function.sort by derick

From: Date: Wed, 31 Aug 2005 10:41:41 +0000
Subject: note 53574 deleted from function.sort by derick
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-94511@lists.php.net to get a copy of this message
Note Submitter: topera at gmail dot com ---- I created a function that reorganize fields in an array. Probably will you use this after a SQL query, with a function like pg_fetch_all(). <?php // how to use $array[0][name] = "Rafael"; $array[0][country] = "Brazil"; $array[1][name] = "Bob"; $array[1][country] = "USA"; echo "Before:"; echo "<pre>"; print_r($array); echo "</pre>"; $array = array_reorganize_fields($array, "country,name"); echo "After:"; echo "<pre>"; print_r($array); echo "</pre>"; // ----------------------------------------------------- // This function reorgazine an array // Author: Rafael Pereira dos Santos // ----------------------------------------------------- function array_reorganize_fields($array, $fields){ $vetor = explode(",", $fields); for($i=0; $i<count($array); $i++) { foreach ($vetor as $field) { $return[$i][$field] = $array[$i][$field]; } } return $return; } ?> This will show: Before: Array ( [0] => Array ( [name] => Rafael [country] => Brazil ) [1] => Array ( [name] => Bob [country] => USA ) ) After: Array ( [0] => Array ( [country] => Brazil [name] => Rafael ) [1] => Array ( [country] => USA [name] => Bob ) )

« previous php.notes (#94511) next »