rfc:array_column
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:array_column [2013/01/12 16:45] – Change "0-indexed number" to "integer key" ramsey | rfc:array_column [2013/01/14 16:38] – Removed array_pluck() alias ramsey | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Request for Comments: array_column ====== | ====== Request for Comments: array_column ====== | ||
- | * Version: 2.2 | + | * Version: 2.4 |
* Date: 2013-01-11 | * Date: 2013-01-11 | ||
* Author: Ben Ramsey < | * Author: Ben Ramsey < | ||
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 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 | ||
) | ) | ||
+ | */ | ||
</ | </ | ||
Line 168: | Line 245: | ||
* 2.1 (2013-01-11): | * 2.1 (2013-01-11): | ||
* 2.2 (2013-01-11): | * 2.2 (2013-01-11): | ||
+ | * 2.3 (2013-01-12): | ||
+ | * 2.4 (2013-01-14): |
rfc/array_column.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1