0
我已經陣列命名$result_for_json_all
php。 2維數組轉換成一維
Array
(
[0] => Array
(
)
[1] => Array
(
[0] => Array
(
[CurrencyAbbreviation] => AUD
[DateOfCurrencyRate] => 2012-12-11
[NumberOfInputRow] => 1
[FinalCurrencyRate] => 0.571
)
)
[2] => Array
(
[0] => Array
(
[CurrencyAbbreviation] => CAD
[DateOfCurrencyRate] => 2013-12-22
[NumberOfInputRow] => 4
[FinalCurrencyRate] => 0.48
)
[1] => Array
(
[NumberOfInputRow] => 2
[FinalCurrencyRate] =>
)
[2] => Array
(
[NumberOfInputRow] => 3
[FinalCurrencyRate] =>
)
)
)
要在此基礎上https://stackoverflow.com/a/9416851/2118559創建的代碼
function flatten($result_for_json_all) {
$arr = array();
foreach($result_for_json_all as $k => $v) {
if(is_array($v)) {
$arr = array_merge($arr, flatten($v, $k));
}
else{
$arr[$k] = $v;
}
}
return $arr;
}
echo '<pre>';
print_r(flatten($result_for_json_all));
echo 'result_for_json_all<pre>';
得到
Array
(
[0] => Array
(
[CurrencyAbbreviation] => AUD
[DateOfCurrencyRate] => 2012-12-11
[NumberOfInputRow] => 1
[FinalCurrencyRate] => 0.571
)
[1] => Array
(
[CurrencyAbbreviation] => CAD
[DateOfCurrencyRate] => 2013-12-22
[NumberOfInputRow] => 4
[FinalCurrencyRate] => 0.48
)
[2] => Array
(
[NumberOfInputRow] => 2
[FinalCurrencyRate] =>
)
[3] => Array
(
[NumberOfInputRow] => 3
[FinalCurrencyRate] =>
)
)
但得到
Array
(
[CurrencyAbbreviation] => AUD
[DateOfCurrencyRate] => 2013-12-22
[NumberOfInputRow] => 3
[FinalCurrencyRate] =>
)
請,建議什麼需要糾正以獲取必要的結果
其他,這是怎麼從我的答案有什麼不同? –