合并具有相同元素的两个数组元素

时间:2015-07-20 09:25:48

标签: php arrays

通过api调用我有2个数组。

    $array1 = array(
    0 => array(
        'rest_id' => 8,
        'price_rates' => Array(
                       'menu' => Array(
                                        0 => Array(
                                            'menu_id' => 'm11',
                                            'price' => 100
                                        )                
                                ),
                        'comparision' => 'superior'
        )
    ),
    1 => array(
        'rest_id' => 9,
        'price_rates' => Array(
                                'menu' => Array(
                                                0 => Array(
                                                    'menu_id' => 'm22',
                                                    'price' => 80
                                                )              
                                ),
            'comparision' => 'medium'
        )
    ),
    2 => array(
        'rest_id' => 400,
        'price_rates' => Array(
                                'menu' => Array(
                                                0 => Array(
                                                    'menu_id' => 'm401',
                                                    'price' => 33
                                                )                
                                ),
            'comparision' => 'low'
        )
    )
);

    $array2 = array(
    0 => array(
        'address' => 'No.12,XXX country',   
        'rest_id' => 400    
    ),
    1 => array(
        'address' => 'No.18,XYZ country',   
        'rest_id' => 8    
    )
);

我想将这两个数组合并为仅包含在第二个数组中的'rest_id'。并且所需的输出是

$result_array = array(
0 => array(
    'rest_id' => 8,
    'price_rates' => Array(
                   'menu' => Array(
                                    0 => Array(
                                        'menu_id' => 'm11',
                                        'price' => 100
                                    )                
                            ),
                    'comparision' => 'superior'
    ),
    'address' => 'No.18,XYZ country'
),
1 => array(
    'rest_id' => 400,
    'price_rates' => Array(
                            'menu' => Array(
                                            0 => Array(
                                                'menu_id' => 'm401',
                                                'price' => 33
                                            )                
                            ),
        'comparision' => 'low'
    ),
    'address' => 'No.12,XXX country'
)

);

有可能吗?我搜索过类似的问题。但是找不到答案。任何帮助,将不胜感激。感谢..

2 个答案:

答案 0 :(得分:0)

我用自定义循环解决了它作为评论建议。

T[]

虽然'rest_id'元素在array_merge_recursive()函数的结果数组coz中将字符串更改为数组,但我现在可以轻松地管理它。

$result = array();

foreach($array1 as $values)
{
foreach($array2 as $values2)
{
    if($values['rest_id'] == $values2['rest_id'])
    {
        $result[] = array_merge_recursive($values, $values2);
        break;
    }
}
}

感谢您提出的所有建议和答案:)

答案 1 :(得分:-1)

这段代码可以解决问题:

foreach($array2 as $key=>$val) {
    $array1[$key]['address'] = $val;
}

(这将导致$array1包括所有值)