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 15:22] – ramsey | rfc:array_column [2013/01/12 17:42] – ramsey | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Request for Comments: array_column ====== | ====== Request for Comments: array_column ====== | ||
- | * Version: 2.2 | + | * Version: 2.3 |
* Date: 2013-01-11 | * Date: 2013-01-11 | ||
* Author: Ben Ramsey < | * Author: Ben Ramsey < | ||
Line 32: | Line 32: | ||
**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 64: | ||
' | ' | ||
' | ' | ||
+ | ), | ||
+ | array( | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
) | ) | ||
); | ); | ||
Line 79: | Line 84: | ||
[1] => Sally | [1] => Sally | ||
[2] => Jane | [2] => Jane | ||
+ | [3] => Peter | ||
) | ) | ||
</ | </ | ||
Line 111: | Line 117: | ||
<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 130: | ||
[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 248: | ||
* 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): |
rfc/array_column.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1