note 41433 added to function.array-map
| From: | bishop at rn2 dot php dot net | Date: | Fri, 09 Apr 2004 21:07:03 +0000 |
| Subject: | note 41433 added to function.array-map | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-67925@lists.php.net to get a copy of this message | ||
Occasionally, you may find that you need to pull out a column (or several) from an array.
Here's a map-like function to do that:
<?php
function &array_shear(&$arrays, $idx1 /* ... */) {
$indexes = func_get_args();
array_shift($indexes);
$newArrays = array ();
foreach (array_keys($arrays) as $arrayKey) {
$newArray = array ();
foreach ($indexes as $index) {
$newArray[$index] = $arrays[$arrayKey][$index];
unset($arrays[$arrayKey][$index]);
}
$newArrays[$arrayKey] = $newArray;
}
return $newArrays;
}
?>
So, doing this:
<?php
$t1 = array (
2 => array ('a', 'b', 'c'),
1 => array ('d', 'e', 'f'),
5 => array ('g', 'h', 'i'),
);
$t2 = array_shear($t1, 1, 0);
?>
will result in:
<?php
$t1 = array (
2 => array ( 2 => 'c', ),
1 => array ( 2 => 'f', ),
5 => array ( 2 => 'i', ),
);
$t2 = array (
2 => array ( 1 => 'b', 0 => 'a', ),
1 => array ( 1 => 'e', 0 => 'd', ),
5 => array ( 1 => 'h', 0 => 'g', ),
);
?>
----
Manual Page -- http://www.php.net/manual/en/function.array-map.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+41433
Delete -- http://master.php.net/manage/user-notes.php?action=delete+41433&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+41433&report=yes
Search -- http://master.php.net/manage/user-notes.php