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 [2013/01/11 23:47] – Updated to reflect mailing list and pull request feedback. 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: 2.0 | + | * Version: 2.4 |
* Date: 2013-01-11 | * 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 18: | Line 18: | ||
array array_column(array $input, mixed $columnKey[, | array array_column(array $input, mixed $columnKey[, | ||
- | array array_pluck(array $input, mixed $columnKey[, | ||
'' | '' | ||
- | |||
- | '' | ||
==== Parameters ==== | ==== Parameters ==== | ||
Line 32: | Line 29: | ||
**columnKey** | **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** | **indexKey** | ||
- | > (Optional.) The column to use as the index/keys for the returned array. This value may be the 0-indexed number | + | > (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 64: | Line 61: | ||
' | ' | ||
' | ' | ||
+ | ), | ||
+ | array( | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
) | ) | ||
); | ); | ||
Line 79: | Line 81: | ||
[1] => Sally | [1] => Sally | ||
[2] => Jane | [2] => Jane | ||
+ | [3] => Peter | ||
) | ) | ||
</ | </ | ||
Line 111: | Line 114: | ||
<code php> | <code php> | ||
<?php | <?php | ||
- | // Array representing a possible record set returned from a database | + | // Using the $records array from Example #1 |
- | $records | + | |
- | array( | + | |
- | ' | + | |
- | ' | + | |
- | ' | + | |
- | ), | + | |
- | array( | + | |
- | ' | + | |
- | ' | + | |
- | ' | + | |
- | ), | + | |
- | array( | + | |
- | ' | + | |
- | ' | + | |
- | ' | + | |
- | ) | + | |
- | ); | + | |
$lastNames = array_column($records, | $lastNames = array_column($records, | ||
print_r($lastNames); | print_r($lastNames); | ||
Line 142: | Line 127: | ||
[3245] => Smith | [3245] => Smith | ||
[5342] => Jones | [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:// |
+ | |||
+ | ===== Mailing list discussion ===== | ||
+ | |||
+ | The mailing list discussion is available [[http:// | ||
+ | |||
+ | ===== 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 ===== | ===== Changelog ===== | ||
Line 153: | Line 243: | ||
* 1.0 (2012-06-21): | * 1.0 (2012-06-21): | ||
* 2.0 (2013-01-11): | * 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.1357948060.txt.gz · Last modified: 2017/09/22 13:28 (external edit)