我有一个包含4个数组的数组,每个数组都有一个值。
array(4) {
[0]=>
array(1) {
["email"]=>
string(19) "test01@testmail.com"
}
[1]=>
array(1) {
["email"]=>
string(19) "test02@testmail.com"
}
[2]=>
array(1) {
["email"]=>
string(19) "test03@testmail.com"
}
[3]=>
array(1) {
["email"]=>
string(19) "test04@testmail.com"
}
}
什么是最好的(=最短的,本机PHP函数首选)方法来展平数组,使其只包含电子邮件地址作为值:
array(4) {
[0]=>
string(19) "test01@testmail.com"
[1]=>
string(19) "test02@testmail.com"
[2]=>
string(19) "test03@testmail.com"
[3]=>
string(19) "test04@testmail.com"
}
答案 0 :(得分:10)
在PHP 5.5中,您有array_column
:
$plucked = array_column($yourArray, 'email');
否则,请使用array_map
:
$plucked = array_map(function($item){ return $item['email'];}, $yourArray);
答案 1 :(得分:1)
您可以使用RecursiveArrayIterator
。这甚至可以使多嵌套数组变得平坦。
<?php
$arr1=array(0=> array("email"=>"test01@testmail.com"),1=>array("email"=>"test02@testmail.com"),2=> array("email"=>"test03@testmail.com"),
3=>array("email"=>"test04@testmail.com"));
echo "<pre>";
$iter = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr1));
$new_arr = array();
foreach($iter as $v) {
$new_arr[]=$v;
}
print_r($new_arr);
<强> OUTPUT:
强>
Array
(
[0] => test01@testmail.com
[1] => test02@testmail.com
[2] => test03@testmail.com
[3] => test04@testmail.com
)