rfc:array_column
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:array_column [2012/06/21 19:19] – created ramsey | rfc:array_column [2013/01/11 23:47] – Updated to reflect mailing list and pull request feedback. ramsey | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Request for Comments: array_column ====== | ====== Request for Comments: array_column ====== | ||
- | * Version: | + | * Version: |
- | * Date: 2012-06-21 | + | * Date: 2013-01-11 |
* Author: Ben Ramsey < | * Author: Ben Ramsey < | ||
* Status: Under Discussion | * Status: Under Discussion | ||
Line 17: | Line 17: | ||
==== Description ==== | ==== Description ==== | ||
- | array array_column(array $input, mixed $key) | + | array array_column(array $input, mixed $columnKey[, mixed $indexKey]) |
+ | array array_pluck(array $input, mixed $columnKey[, | ||
- | '' | + | '' |
+ | |||
+ | '' | ||
==== Parameters ==== | ==== Parameters ==== | ||
Line 27: | Line 30: | ||
> A multi-dimensional array (record set) from which to pull a column of values. | > A multi-dimensional array (record set) from which to pull a column of values. | ||
- | **key** | + | **columnKey** |
> The column of values to return. This value may be the 0-indexed number of the column you wish to retrieve, or it may be the string key name for an associative array. | > The column of values to return. This value may be the 0-indexed number of the column you wish to retrieve, or it may be the string key name for an associative array. | ||
+ | |||
+ | **indexKey** | ||
+ | |||
+ | > (Optional.) The column to use as the index/keys for the returned array. This value may be the 0-indexed number of the column, or it may be the string key name. | ||
==== Return Values ==== | ==== Return Values ==== | ||
Line 44: | Line 51: | ||
$records = array( | $records = array( | ||
array( | array( | ||
- | ' | + | ' |
' | ' | ||
' | ' | ||
), | ), | ||
array( | array( | ||
- | ' | + | ' |
' | ' | ||
' | ' | ||
), | ), | ||
array( | array( | ||
- | ' | + | ' |
' | ' | ||
' | ' | ||
Line 85: | Line 92: | ||
); | ); | ||
- | $firstNames | + | $lastNames |
- | print_r($firstNames); | + | print_r($lastNames); |
</ | </ | ||
Line 97: | Line 104: | ||
[1] => Smith | [1] => Smith | ||
[2] => Jones | [2] => Jones | ||
+ | ) | ||
+ | </ | ||
+ | |||
+ | === Example #3: Get column of last names from recordset, indexed by the " | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | // Array representing a possible record set returned from a database | ||
+ | $records = array( | ||
+ | array( | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ), | ||
+ | array( | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ), | ||
+ | array( | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ) | ||
+ | ); | ||
+ | |||
+ | $lastNames = array_column($records, | ||
+ | print_r($lastNames); | ||
+ | </ | ||
+ | |||
+ | The above example will output: | ||
+ | |||
+ | < | ||
+ | Array | ||
+ | ( | ||
+ | [2135] => Doe | ||
+ | [3245] => Smith | ||
+ | [5342] => Jones | ||
) | ) | ||
</ | </ | ||
Line 107: | Line 152: | ||
* 1.0 (2012-06-21): | * 1.0 (2012-06-21): | ||
- | + | * 2.0 (2013-01-11): |
rfc/array_column.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1