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/12 17:17] – Added Example #4 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: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
Line 11: | Line 11: | ||
This RFC proposes a new array function that returns the values of the specified column from a multi-dimensional array. Inspired by database methods like '' | This RFC proposes a new array function that returns the values of the specified column from a multi-dimensional array. Inspired by database methods like '' | ||
- | This has been submitted as a [[https:// | + | This has been submitted as a [[https:// |
===== Specification ===== | ===== Specification ===== | ||
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 integer | ||
+ | |||
+ | **indexKey** | ||
- | > The column | + | > (Optional.) |
==== 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 98: | Line 105: | ||
[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 | ||
+ | ) | ||
+ | </ | ||
+ | |||
+ | === Example #4: Mismatched columns === | ||
+ | |||
+ | With '' | ||
+ | |||
+ | The following examples will all use the same **$mismatchedColumns** array defined here: | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | $mismatchedColumns = array( | ||
+ | array( | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ), | ||
+ | array( | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ), | ||
+ | array( | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ), | ||
+ | ); | ||
+ | </ | ||
+ | |||
+ | In this example, all rows contain an " | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | $foo = array_column($mismatchedColumns, | ||
+ | $bar = array(' | ||
+ | |||
+ | /* | ||
+ | Both $foo and $bar contain values that look like this: | ||
+ | |||
+ | Array | ||
+ | ( | ||
+ | [bar] => foo | ||
+ | [0] => qux | ||
+ | [garply] => grault | ||
+ | ) | ||
+ | */ | ||
+ | </ | ||
+ | |||
+ | However, if we want to retrieve all " | ||
+ | |||
+ | <code php> | ||
+ | // There is a corresponding " | ||
+ | print_r(array_column($mismatchedColumns, | ||
+ | |||
+ | /* | ||
+ | Array | ||
+ | ( | ||
+ | [foo] => bar | ||
+ | [grault] => garply | ||
+ | ) | ||
+ | */ | ||
+ | |||
+ | // There are no corresponding " | ||
+ | print_r(array_column($mismatchedColumns, | ||
+ | |||
+ | /* | ||
+ | Array | ||
+ | ( | ||
+ | [0] => bar | ||
+ | [1] => garply | ||
+ | ) | ||
+ | */ | ||
</ | </ | ||
===== Proposal and Patch ===== | ===== Proposal and Patch ===== | ||
- | The patch (including tests) for this proposal is available in [[https:// | + | The patch (including tests) for this proposal is available in [[https:// |
- | ===== Changelog | + | ===== Mailing list discussion |
- | * 1.0 (2012-06-21): | + | The mailing list discussion |
+ | ===== Voting ===== | ||
+ | Voting ends not before Friday, January 18, 2013. The PHP language is not changed, so a 50% + 1 majority is required. | ||
+ | |||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
+ | |||
+ | ===== Changelog ===== | ||
+ | |||
+ | * 1.0 (2012-06-21): | ||
+ | * 2.0 (2013-01-11): | ||
+ | * 2.1 (2013-01-11): | ||
+ | * 2.2 (2013-01-11): |
rfc/array_column.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1