2011-09-27 223 views
2

我正在追逐我的尾巴,嘗試將兩個不同查詢的結果合併到一個模板中輸出。合併兩個多維關聯數組

我想合併model_data和entry_data中的相應子數組以獲得desired_result。然後我將迭代desired_result並將值輸入到模板中。

任何幫助,非常感謝。

model_data

array(2) { 
    [0]=> 
    array(2) { 
    ["entry_id"]=> string(3) "192" 
    ["field_id_49"]=> string(10) "Model Name" 
    } 
    [1]=> 
    array(2) { 
    ["entry_id"]=> string(3) "193" 
    ["field_id_49"]=> string(5) "MN123" 
    } 
} 

entry_data

array(2) { 
    [0]=> 
    array(2) { 
    ["uri"]=> string(24) "/products/product-title/" 
    ["title"]=> string(13) "Product Title" 
    } 
    [1]=> 
    array(2) { 
    ["uri"]=> string(22) "/products/lorem-ipsum/" 
    ["title"]=> string(11) "Lorem Ipsum" 
    } 
} 

desired_result

array(2) { 
    [0]=> 
    array(4) { 
    ["entry_id"]=> string(3) "192" 
    ["field_id_49"]=> string(10) "Model Name"  
    ["uri"]=> string(24) "/products/product-title/" 
    ["title"]=> string(13) "Product Title"  
    } 
    [1]=> 
    array(4) { 
    ["entry_id"]=> string(3) "193" 
    ["field_id_49"]=> string(5) "MN123"  
    ["uri"]=> string(22) "/products/lorem-ipsum/" 
    ["title"]=> string(11) "Lorem Ipsum" 
    } 
} 
+0

你確定你想要的結果?我實際上無法理解邏輯。 –

+0

我需要將entry_data [0],model_data [1]與entry_data [1]合併到model_data [0]。兩者都會繼續添加數組,所以數組鍵需要增加。 – juddlyon

回答

7
foreach($model_data as $key => $value){ 
    $result[$key] = array_merge($entry_data[$key], $model_data[$key]); 
} 
+0

完美,就是我以後的樣子。在盯着我的屏幕太長時間後,我過於複雜。謝謝! – juddlyon

+0

@juddlyon很高興我幫助 – amosrivera

+0

只適用於二維數組。雖然對我來說足夠好。 – ProblemsOfSumit