我遇到了一个问题。我有一个看起来像这样的数组
print_r($log_ret_val);
Array ( [0] => Array ( [0] => Array ( [TestingLogDevice] => Array ( [Siteid] => Mirpur_CO ) )
[1] => Array ( [TestingLogDevice] => Array ( [Siteid] => Mirpur_CO ) )
)
[1] => Array ( [0] => Array ( [TestingLogDevice] => Array ( [Tempin] => 29 ) )
[1] => Array ( [TestingLogDevice] => Array ( [Tempin] => 29 ) )
)
[2] => Array ( [0] => Array ( [TestingLogDevice] => Array ( [Date_time] => 18.11.2017 11:03:33 ) )
[1] => Array ( [TestingLogDevice] => Array ( [Date_time] => 18.11.2017 11:00:31 ) )
)
)
这就是我为该值所写的内容
$log_val = array();
foreach ($log_ret_val as $key => $valuee) {
foreach ($valuee as $key => $val) {
array_push($log_val,$val);
}
}
print_r($log_val);
Array ( [0] => Array ( [TestingLogDevice] => Array ( [Siteid] => Mirpur_CO ) )
[1] => Array ( [TestingLogDevice] => Array ( [Siteid] => Mirpur_CO ) )
[2] => Array ( [TestingLogDevice] => Array ( [Tempin] => 29 ) )
[3] => Array ( [TestingLogDevice] => Array ( [Tempin] => 28 ) )
[4] => Array ( [TestingLogDevice] => Array ( [Date_time] => 18.11.2017 11:24:45 ) )
[5] => Array ( [TestingLogDevice] => Array ( [Date_time] => 18.11.2017 11:21:43 ) )
)
但我希望的输出看起来像这样
Array ( [0] => Array ( [TestingLogDevice] => Array ( [Siteid] => Mirpur_CO [Tempin] => 29 [Date_time] => 18.11.2017 11:24:45 ) )
[1] => Array ( [TestingLogDevice] => Array ( [Siteid] => Mirpur_CO [Tempin] => 28 [Date_time] => 18.11.2017 11:21:43 ) )
)
那么我能做些什么才能满足我想要的输出。请您提出建议或帮助
答案 0 :(得分:2)
循环遍历$log_ret_val
提取密钥并使用array_merge_recursive
创建新数组
$log_ret_val = Array ( "0" => Array ( "0" => Array ( "TestingLogDevice" => Array ( "Siteid" => "Mirpur_CO" ) ) ,
"1" => Array ( "TestingLogDevice" => Array ( "Siteid" => "Mirpur_CO" ) ) ,
) ,
"1" => Array ( "0" => Array ( "TestingLogDevice" => Array ( "Tempin" => 29 ) ) ,
"1" => Array ( "TestingLogDevice" => Array ( "Tempin" => 29 ) ) ,
) ,
"2" => Array ( "0" => Array ( "TestingLogDevice" => Array ( "Date_time" => "18.11.2017 11:03:33" ) ) ,
"1" => Array ( "TestingLogDevice" => Array ( "Date_time" => "18.11.2017 11:00:31" ) )
)
);
$log_val = array();
foreach ($log_ret_val as $key => $value) {
foreach ($value as $key1 => $value1) {
if(isset($log_val[$key1]))
$log_val[$key1] = array_merge_recursive($log_val[$key1],$value1);
else
$log_val[$key1] = array_merge_recursive($value1);
}
}
print_r($log_val);