note 54115 added to function.array-slice
| From: | david dot tulloh at infaze dot com dot au | Date: | Fri, 24 Jun 2005 01:26:23 +0000 |
| Subject: | note 54115 added to function.array-slice | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-90872@lists.php.net to get a copy of this message | ||
Nice one liner to extract a column from a 2D array.
It works by using array_slice on every row, through array_map.
<?php
// set up a small test environment
$test_subject[] = array("a", "b", "c");
$test_subject[] = array("d", "e", "f");
$column=1;
// do the actual work
$result = array_map('array_slice', $test_subject,
array_fill(0, count($test_subject), $column),
array_fill(0, count($test_subject), 1)
);
// and the end result
result == array ( array("b"), array("e") );
?>
----
Manual Page -- http://www.php.net/manual/en/function.array-slice.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+54115
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54115&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54115&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54115&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54115&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54115&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54115&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+54115&report=yes
Search -- http://master.php.net/manage/user-notes.php