array(array(“page” => “page1”, “name” => “pagename1”) array(“page” => “page2”, “name” => “pagename2”) array(“page” => “page3”, “name” => “pagename3”))Is there a built-in function that will return a new array with just the values of the “name” keys? so I”d get:

array(“pagename1”, “pagename2”, “pagename3”)

Đang xem: Is there a function to extract a 'column' from an array in php?

*

*

Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first)
As of PHP 5.5 you can use array_column():

“page1”, “name” => “pagename1”), array(“page” => “page2”, “name” => “pagename2”), array(“page” => “page3”, “name” => “pagename3”) );$names = array_column($samples, “name”);print_r($names);See it in action

*

Why does it have to be a built in function? No, there is none, write your own.

Here is a nice and easy one, as opposed to others in this thread.

$namearray = array();foreach ($array as $item) { $namearray<> = $item<"name">;}In some cases where the keys aren”t named you could instead do something like this

$namearray = array();foreach ($array as $key => $value) { $namearray <> = $value;}

*

*

Here”s a functional way of doing it:

$data = array( array(“page” => “page1”, “name” => “pagename1”), array(“page” => “page2”, “name” => “pagename2”), array(“page” => “page3”, “name” => “pagename3”));$result = array_map(create_function(“$arr”, “return $arr<"name">;”), $data);print_r($result);
Well there is. At least for PHP > 5.5.0 and it is called array_column

The PHP function takes an optional $index_keyparameter that – as per the PHP website – states:

$index_key

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

In the answers here, i see a stripped version without the optional parameter.I needed it, so, here is the complete function:

if (!function_exists(“array_column”)) { function array_column($array, $column, $index_key = null) { $toret = array(); foreach ($array as $key => $value) { if ($index_key === null){ $toret<> = $value<$column>; }else{ $toret<$value<$index_key>> = $value<$column>; } } return $toret; }}
Share
Improve this answer
Follow
answered Jan 15, 2015 at 0:29
papas-sourcepapas-source
1,11911 gold badge1313 silver badges1919 bronze badges
Add a comment |
1
Similar to fuentesjrs solution, but a bit more generic using array_walk() with a custom callback:

// Define the callbackfunction extract_named_sub_elements(&$item, $key, $name) { $item = $item<$name>;}// Test data$original = array( array(“page” => “page1”, “name” => “pagename1”), array(“page” => “page2”, “name” => “pagename2”), array(“page” => “page3”, “name” => “pagename3”),);// Use a copy, as array_walk() operates directly on the passed in array$copy = $original;// Substitute “name” with whatever element you want to extract, e.g. “page”array_walk($copy, “extract_named_sub_elements”, “name”);print_r($copy);
Share
Improve this answer
Follow
answered Sep 29, 2009 at 21:23
Henrik OpelHenrik Opel
19.3k11 gold badge4747 silver badges6262 bronze badges
Add a comment |
1
if (!function_exists(“array_column”)) { function array_column($array,$column) { $col = array(); foreach ($array as $k => $v) { $col<>=$v<$column>; } return $col; }}This should work for php versions

*

*

*
*

Xem thêm: Sau Ung Thư Trực Tràng Tnm, Các Giai Đoạn Ung Thư Đại Trực Tràng

*

*

*
*

*
*

Xem thêm:

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *