我有內部數組的數組:地圖二維PHP數組1維
{
"0" => array("key" => "code", "id" => "4", "value" => "yes"),
"1" => array("key" => "parameter", "id" => "4", "value" => "0"),
"2" => array("key" => "code", "id" => "5", "value" => "no"),
etc...
}
這就是我想做的事:我想有一維數組,其中的關鍵是「ID」和值會是「價值」。但是,我需要過濾出其關鍵字爲「參數」的條目。所以,在這個例子中,最終的陣列應該是這樣的:
{
"4" => "yes",
"5" => "no"
}
我似乎無法弄清楚如何做到這一點。你能幫我一下嗎?我試圖在foreach中編寫這個foreach,但是我無法圍繞如何過濾數據。
foreach ($settings AS $key => $value) {
$id = null;
$value = null;
foreach ($value AS $key2 => $value2) {
// No idea how to filter out uneccesary entries and save the correct ones
}
$finalArray[$id] = $value;
}
使用[array_filter()](http://php.net/manual/en/function.array-filter.php)刪除'key'是'parameter'的條目;那麼[array_column()](http://php.net/manual/en/function.array-column.php)對這個結果:'$ finalArray = array_column(array_filter($ settings,function($ value){返回$ value ['key']!=='parameter';}),'id','value');' –
@Marius試試這個:https://stackoverflow.com/a/45921364/4369087 – yoeunes