我有这个包含2个键和值的数组..
array (
23 =>
array (
0 => 9,
1 => 13,
2 => 2,
3 => 11,
4 => 4,
5 => 5,
6 => 6,
7 => 12,
8 => 1,
9 => 7,
10 => 10,
11 => 8,
12 => 3,
),
1 =>
array (
0 => 9,
1 => 13,
2 => 2,
3 => 11,
4 => 4,
5 => 5,
6 => 6,
7 => 14,
8 => 12,
9 => 1,
10 => 7,
11 => 10,
12 => 8,
13 => 3,
)
)
那么如何将此转换为具有不同值的单个查询
array (
0 => 9,
1 => 13,
2 => 2,
3 => 11,
4 => 4,
5 => 5,
6 => 6,
7 => 12,
8 => 1,
9 => 7,
10 => 10,
11 => 8,
12 => 3,
20 => 14,
)
这意味着它应首先合并然后创建不同的值数组而不使用更多 foreach / for循环。
这是我尝试过的代码http://codepad.org/x881cBt1
答案 0 :(得分:1)
您可以使用array_walk_recursive展平您的多维数组:
$flatten = [];
array_walk_recursive($array, function ($value) use (&$flatten) {
$flatten[] = $value;
});
$flatten = array_unique($flatten); //Taking Unique for the flattened array
print_r($flatten);
这应该给出:
Array
(
[0] => 9
[1] => 13
[2] => 2
[3] => 11
[4] => 4
[5] => 5
[6] => 6
[7] => 12
[8] => 1
[9] => 7
[10] => 10
[11] => 8
[12] => 3
[20] => 14
)
检查EVAL
答案 1 :(得分:0)
<?php
$data; //Your array
$data_set = array_values($data);
$required_data = [];
for ($i=0; $i< count($data_set); $i++) {
$required_data = array_merge($required_data, $data_set[$i]);
unset($data_set[$i]);
}
var_dump($required_data);
这将解决问题
答案 2 :(得分:0)
您可以使用此代码,因为您说此代码没有任何循环
$myArray = array (
23 =>
array (
0 => 9,
1 => 13,
2 => 2,
3 => 11,
4 => 4,
5 => 5,
6 => 6,
7 => 12,
8 => 1,
9 => 7,
10 => 10,
11 => 8,
12 => 3,
),
1 =>
array (
0 => 9,
1 => 13,
2 => 2,
3 => 11,
4 => 4,
5 => 5,
6 => 6,
7 => 14,
8 => 12,
9 => 1,
10 => 7,
11 => 10,
12 => 8,
13 => 3,
)
);
$objTmp = (object) array('array' => array());
array_walk_recursive($myArray, create_function('&$v, $k, &$t', '$t->array[] = $v;'), $objTmp);
print_r (array_unique($objTmp->array));
/* output
Array
(
[0] => 9
[1] => 13
[2] => 2
[3] => 11
[4] => 4
[5] => 5
[6] => 6
[7] => 12
[8] => 1
[9] => 7
[10] => 10
[11] => 8
[12] => 3
[20] => 14
)
*/
谢谢..
答案 3 :(得分:0)
$arr;//Your array
$final_arr=array();
foreach($arr as $r){
$r=array_unique($r);
$final_arr=array_merge($final_arr,$r);
}
$final_arr=array_unique($final_arr);
print_r($final_arr);
直播ideone
答案 4 :(得分:0)
//Here 1st you have to Merge array & then remove duplicate entry
<?php
$main_array=array("0"=>array(1,2,3,4),"1"=>array(2,6,4),"2"=>array(2,8,5));
$temp_array=array();
for($i=0;$i<count($main_array);$i++)
{
$temp_array=array_merge($temp_array,$main_array[$i]);
}
//var_dump($temp_array);
$final_array=array_unique($temp_array);
echo "array:";
var_dump($final_array);
?>