rfc:array_column
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:array_column [2012/06/21 19:35] – [Examples] Updating variable name in Example #2 ramsey | rfc:array_column [2017/09/22 13:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
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]) |
- | '' | + | '' |
==== Parameters ==== | ==== Parameters ==== | ||
Line 27: | Line 27: | ||
> 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 | + | > The column of values to return. This value may be the integer key 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 integer key of the column, or it may be the string key name. | ||
==== Return Values ==== | ==== Return Values ==== | ||
Line 44: | Line 48: | ||
$records = array( | $records = array( | ||
array( | array( | ||
- | ' | + | ' |
' | ' | ||
' | ' | ||
), | ), | ||
array( | array( | ||
- | ' | + | ' |
' | ' | ||
' | ' | ||
), | ), | ||
array( | array( | ||
- | ' | + | ' |
' | ' | ||
' | ' | ||
+ | ), | ||
+ | array( | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
) | ) | ||
); | ); | ||
Line 72: | Line 81: | ||
[1] => Sally | [1] => Sally | ||
[2] => Jane | [2] => Jane | ||
+ | [3] => Peter | ||
) | ) | ||
</ | </ | ||
Line 98: | Line 108: | ||
[2] => Jones | [2] => Jones | ||
) | ) | ||
+ | </ | ||
+ | |||
+ | === Example #3: Get column of last names from recordset, indexed by the " | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | // Using the $records array from Example #1 | ||
+ | $lastNames = array_column($records, | ||
+ | print_r($lastNames); | ||
+ | </ | ||
+ | |||
+ | The above example will output: | ||
+ | |||
+ | < | ||
+ | Array | ||
+ | ( | ||
+ | [2135] => Doe | ||
+ | [3245] => Smith | ||
+ | [5342] => Jones | ||
+ | [5623] => Doe | ||
+ | ) | ||
+ | </ | ||
+ | |||
+ | === 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 | ||
+ | ) | ||
+ | */ | ||
+ | </ | ||
+ | |||
+ | === Example #5: indexKey Collisions === | ||
+ | |||
+ | In the event that more than one row contains the same value for // | ||
+ | |||
+ | <code php> | ||
+ | // Using the $records array from Example #1 | ||
+ | $firstNames = array_column($records, | ||
+ | print_r($firstNames); | ||
+ | |||
+ | /* | ||
+ | Array | ||
+ | ( | ||
+ | [Doe] => Peter | ||
+ | [Smith] => Sally | ||
+ | [Jones] => Jane | ||
+ | ) | ||
+ | */ | ||
</ | </ | ||
===== 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): | ||
+ | * 2.3 (2013-01-12): | ||
+ | * 2.4 (2013-01-14): |
rfc/array_column.1340307321.txt.gz · Last modified: 2017/09/22 13:28 (external edit)