rfc:array_column

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

rfc:array_column [2013/01/11 23:58]
ramsey Adding link to new pull request
rfc:array_column [2017/09/22 13:28]
Line 1: Line 1:
-====== Request for Comments: array_column ====== 
  
-  * Version: 2.1 
-  * Date: 2013-01-11 
-  * Author: Ben Ramsey <​ramsey@php.net>​ 
-  * Status: Under Discussion 
-  * First Published at: http://​wiki.php.net/​rfc/​array_column 
- 
-===== Introduction ===== 
- 
-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 ''​PDOStatement::​fetchColumn()'',​ ''​array_column()''​ moves useful functionality into the core that once had to be implemented in userland code with sometimes complex loops. 
- 
-This has been submitted as a [[https://​github.com/​php/​php-src/​pull/​257|pull request on GitHub]], where there has already been a significant amount of discussion. 
- 
-===== Specification ===== 
- 
-==== Description ==== 
- 
-  array array_column(array $input, mixed $columnKey[,​ mixed $indexKey]) 
-  array array_pluck(array $input, mixed $columnKey[,​ mixed $indexKey]) 
- 
-''​array_column()''​ returns the values from a single column of the //​**input**//​ array, identified by the //​**columnKey**//​. Optionally, you may provide an //​**indexKey**//​ to index the values in the returned array by the values from the //​**indexKey**//​ column in the //​**input**//​ array. 
- 
-''​array_pluck()''​ is an alias of ''​array_column()''​. 
- 
-==== Parameters ==== 
- 
-**input** 
- 
-> A multi-dimensional array (record set) from which to pull a column of values. 
- 
-**columnKey** 
- 
-> The column of values to return. This value may be the 0-indexed number 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 0-indexed number of the column, or it may be the string key name. 
- 
-==== Return Values ==== 
- 
-Returns an array of values representing a single column from the input array. 
- 
-==== Examples ==== 
- 
-=== Example #1: Get column of first names from recordset === 
- 
-<code php> 
-<?php 
-// Array representing a possible record set returned from a database 
-$records = array( 
-    array( 
-        '​id'​ => 2135, 
-        '​first_name'​ => '​John',​ 
-        '​last_name'​ => '​Doe'​ 
-    ), 
-    array( 
-        '​id'​ => 3245, 
-        '​first_name'​ => '​Sally',​ 
-        '​last_name'​ => '​Smith'​ 
-    ), 
-    array( 
-        '​id'​ => 5342, 
-        '​first_name'​ => '​Jane',​ 
-        '​last_name'​ => '​Jones'​ 
-    ) 
-); 
- 
-$firstNames = array_column($records,​ '​first_name'​);​ 
-print_r($firstNames);​ 
-</​code>​ 
- 
-The above example will output: 
- 
-<​code>​ 
-Array 
-( 
-    [0] => John 
-    [1] => Sally 
-    [2] => Jane 
-) 
-</​code>​ 
- 
-=== Example #2: Retrieve a column of values from a numerically-indexed array === 
- 
-<code php> 
-<?php 
-$records = array( 
-    array(1, '​John',​ '​Doe'​),​ 
-    array(2, '​Sally',​ '​Smith'​),​ 
-    array(3, '​Jane',​ '​Jones'​) 
-); 
- 
-$lastNames = array_column($records,​ 2); 
-print_r($lastNames);​ 
-</​code>​ 
- 
-The above example will output: 
- 
-<​code>​ 
-Array 
-( 
-    [0] => Doe 
-    [1] => Smith 
-    [2] => Jones 
-) 
-</​code>​ 
- 
-=== Example #3: Get column of last names from recordset, indexed by the "​id"​ column === 
- 
-<code php> 
-<?php 
-// Array representing a possible record set returned from a database 
-$records = array( 
-    array( 
-        '​id'​ => 2135, 
-        '​first_name'​ => '​John',​ 
-        '​last_name'​ => '​Doe'​ 
-    ), 
-    array( 
-        '​id'​ => 3245, 
-        '​first_name'​ => '​Sally',​ 
-        '​last_name'​ => '​Smith'​ 
-    ), 
-    array( 
-        '​id'​ => 5342, 
-        '​first_name'​ => '​Jane',​ 
-        '​last_name'​ => '​Jones'​ 
-    ) 
-); 
- 
-$lastNames = array_column($records,​ '​last_name',​ '​id'​);​ 
-print_r($lastNames);​ 
-</​code>​ 
- 
-The above example will output: 
- 
-<​code>​ 
-Array 
-( 
-    [2135] => Doe 
-    [3245] => Smith 
-    [5342] => Jones 
-) 
-</​code>​ 
- 
-===== Proposal and Patch ===== 
- 
-The patch (including tests) for this proposal is available in [[https://​github.com/​php/​php-src/​pull/​56|GitHub Pull Request #56]]. 
- 
-===== Changelog ===== 
- 
-  * 1.0 (2012-06-21):​ Initial draft, following discussion on on [[https://​github.com/​php/​php-src/​pull/​56|GitHub Pull Request #56]] 
-  * 2.0 (2013-01-11):​ Updated to reflect mailing list and pull request feedback. 
-  * 2.1 (2013-01-11):​ Adding link to new pull request: https://​github.com/​php/​php-src/​pull/​257 
rfc/array_column.txt · Last modified: 2017/09/22 13:28 (external edit)