PHP compare multidimensional array values in single dimensional array then merge the array

时间:2017-07-12 08:12:43

标签: php arrays array-merge

Array 1:

Array
(
    [0] => Array
        (
            [status_id] => 15
            [status_posted_by] => 2
            [status_msg] => 
            [posted_date_and_time] => 2017-06-21 12:22:01
            [image_name] => JO361165.jpg
            [type] => photo
            [share_status] => 0
            [shared_by] => 0
        )

    [1] => Array
        (
            [status_id] => 32
            [status_posted_by] => 2
            [status_msg] => 
            [posted_date_and_time] => 2017-06-21 12:12:51
            [image_name] => JO262000.jpg
            [type] => photo
            [share_status] => 0
            [shared_by] => 0
        )
)

Array 2

Array
(
    [15] => 2
    [24] => 1
)

Expected result is compare array2 to the array1. If array1[status_id]=array2[key] merge like

[status_id] => 15
[status_posted_by] => 2
[status_msg] => 
[posted_date_and_time] => 2017-06-21 12:22:01
[image_name] => JO361165.jpg
[type] => photo
[share_status] => 0
[shared_by] => 0
[like] => 2

2 个答案:

答案 0 :(得分:0)

you can use array_combine( ) function for that it may-helpful to you

array array_combine ( array $keys , array $values );
$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);

答案 1 :(得分:0)

您可以尝试以下代码:

$merged_array = array();

for ($count1 = 0; $count1 < count ($array1); $count1++) {
    for ($count2 = 0; $count2 < count ($array1); $count2++) {
        if ($array1[$count1]['status_id'] == $array2[$count2]['key']) {
            $merged_array[] = array_merge($array1[$count], $array2[$count2]);
        }
    }
}

$ merged_array现在应该具有所需的数据