note 53574 added to function.sort
| From: | topera at gmail dot com | Date: | Mon, 06 Jun 2005 12:29:11 +0000 |
| Subject: | note 53574 added to function.sort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-90006@lists.php.net to get a copy of this message | ||
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
)
)
----
Manual Page -- http://www.php.net/manual/en/function.sort.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+53574
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+53574&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+53574&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+53574&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+53574&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+53574&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+53574&report=yes
Search -- http://master.php.net/manage/user-notes.php