我创建了一个工作函数,用于将后续关联数组拆分为两个数值数组。我想知道是否有更好的方法,避免循环。
我有一个数组如下。
Array ( [0] => stdClass Object ( [CreatedAt] => 16/02/2014 [Occurrence] => 1 ) [1] => stdClass Object ( [CreatedAt] => 17/02/2014 [Occurrence] => 8 ) [2] => stdClass Object ( [CreatedAt] => 18/02/2014 [Occurrence] => 4 ) [3] => stdClass Object ( [CreatedAt] => 20/02/2014 [Occurrence] => 11 ) )
需要将其转换为两个数值数组
Array ( [0] => 16/02/2014 [1] => 17/02/2014 [2] => 18/02/2014 [3] => 20/02/2014 ) Array ( [0] => 1 [1] => 8 [2] => 4 [3] => 11 )
我使用了foreach
循环
$array1 = array(); $array2 = array(); foreach ($mainArray as $key => $value) { $array1[] = $value->CreatedAt; $array2[] = $value->Occurrence; } print_r($array1); print_r($array2);
但是如果我在mainArray中有1000行,则会影响性能。如果您有更好的解决方案,请大家知道。
答案 0 :(得分:0)
试试这个:
$objName = (object) array('aFlat' => array());
array_walk_recursive($array, create_function('&$v, $k, &$t', '$t->aFlat[] = $v;'), $objName);
var_dump($objName->aFlat);
参考: How to "flatten" a multi-dimensional array to simple one in PHP?
答案 1 :(得分:0)
http://in1.php.net/array_column
可能这可以解决你的问题!!