我想子阵列添加到PHP的一个单一的阵列 [英] I want to add sub arrays to one single array in php
问题描述
我有数组这样的.........
i have array like this.........
Array
(
[0] => Array
(
[0] => rose
[1] => monkey
[2] => donkey
)
[1] => Array
(
[0] => daisy
[1] => monkey
[2] => donkey
)
[2] => Array
(
[0] => orchid
[1] => monkey
[2] => donkey
)
)
,我想这样的.........
and i want like this.........
Array
(
[0] => rose
[1] => monkey
[2] => donkey
[3] => daisy
[4] => monkey
[5] => donkey
[6] => orchid
[7] => monkey
[8] => donkey
)
....我用数组合并,但它不工作,因为我的数组产生dymaically每一次表现出不同的阵列。
问题是我不能array_merge动态传递数组()function.It接受阵列只能手动输入,不接受任何其他变量。功能只接受数组。
....I used array merge but it's not working because my array generates dymaically and each time shows different arrays. problem is I can't pass arrays dynamically in array_merge() function.It accepts only manually entries of array and not accepts any other variable .function accepts only array.
它的工作原理是这样...
it works like this ...
$total_data = array_merge($data[0],$data[1],$data[2]);
因为每次它产生阵列的不同数目的动态,以便
我必须使用这样的....
as each time it generates different numbers of array dynamically so i have to use like this....
$data_array = $data[0],$data[1],$data[2];
$total_data = array_merge($data_array);
但它显示了一个错误array_merge()[function.array合并]:参数#1是不是数组......
but it shows an error "array_merge() [function.array-merge]: Argument #1 is not an array"......
推荐答案
试试这个:
$array = your array
$result = call_user_func_array('array_merge', $array);
echo "<pre>";
print_r($result);
或者试试这个:
function array_flatten($array) {
$return = array();
foreach ($array as $key => $value) {
if (is_array($value)){ $return = array_merge($return, array_flatten($value));}
else {$return[$key] = $value;}
}
return $return;
}
$array = Your array
$result = array_flatten($array);
echo "<pre>";
print_r($result);
这篇关于我想子阵列添加到PHP的一个单一的阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!