2011-09-27 119 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