php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #26338 Add function for fetching column from an array
Submitted: 2003-11-20 22:51 UTC Modified: 2013-04-03 18:43 UTC
From: Xuefer at 21cn dot com Assigned: nikic (profile)
Status: Closed Package: *General Issues
PHP Version: 4.3.4 OS: all
Private report: No CVE-ID: None
 [2003-11-20 22:51 UTC] Xuefer at 21cn dot com
Description:
------------
old:
Usage: array array_values ( array input )
Purpose: Return all the values of an array 

new suggestion:
Usage: array array_values ( array input, [mixed key] )
Purpose: Return all the values of an array 

key default to null, which is same as old array_values
if key is_int or is_string

it act like this:
function array_values($input, $key)
{
	$ret = array();
	foreach ($input as $value) {
		$ret[] = $value[$key];
	}
	return $ret;
}

this is very useful to collect ids
e.g.:
$idrows = db_query_all_rows("select id from table1");
$ids = implode(',', array_values($idrows, 'id'));
$myrows = db_query_all_rows("select .... from table2 where id IN($ids)");
(db_query_all_rows is custom function which fetch all rows of query result)
thanks


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-11-24 22:30 UTC] Xuefer at 21cn dot com
if anyone think we should not modify array_values
i have anohter suggested name:
array_column($array, $whichColumn)
because it's mostly used to get all fields(foreah row) in one column
 [2013-03-15 14:56 UTC] nikic@php.net
RFC for this feature was accepted, but not yet merged: https://wiki.php.net/rfc/array_column
 [2013-03-15 14:56 UTC] nikic@php.net
-Summary: 2nd param for array_values +Summary: Add function for fetching column from an array -Package: Feature/Change Request +Package: *General Issues
 [2013-04-03 18:43 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2013-04-03 18:43 UTC] nikic@php.net
array_column has been merged for PHP 5.5.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Mon Jul 22 12:01:25 2019 UTC