doumi7861
doumi7861
2016-03-25 15:18
浏览 58
已采纳

比较两个多维数组的值和插入(如果不存在)

I have two array $array1 and $array2 which I get dynamically and look like

$array1 = Array
    (
        [0] => Array
            (
                [hour] => 10
                [activity] => Array
                    (
                        [0] => Array
                            (
                                [activity_id] => 1
                                [cnt] => 2
                            )
                        [1] => Array
                            (
                                [activity_id] => 2
                                [cnt] => 1
                            )
                    )
            )
        [1] => Array
            (
                [hour] => 11
                [activity] => Array
                    (
                    )
            )
        [2] => Array
            (
                [hour] => 12
                [percentage] => 0
                [activity] => Array
                    (
                        [0] => Array
                            (
                                [activity_id] => 2
                                [cnt] => 5
                            )
                        [1] => Array
                            (
                                [activity_id] => 3
                                [cnt] => 2
                            )
                    )
            )
    );


$array2 = Array
    (
        [0] => Array
            (
                [id] => 1
                [name] => Phone Calls
                [readable] => 1
                [status] => active
            )
        [1] => Array
            (
                [id] => 2
                [name] => Meeting With Customer
                [readable] => 1
                [status] => active
            )
        [2] => Array
            (
                [id] => 3
                [name] => Others Works
                [readable] => 1
                [status] => active
            )
    );

which i need to compare.

if $array2['id'] is not in $array1["activity"](i.e"activity_id") add array ['activity_id'=>$array2['id'],'cnt'=>0] to $array1['activity'].

My result must be like

$result = Array
    (
        [0] => Array
            (
                [hour] => 10
                [activity] => Array
                    (
                        [0] => Array
                            (
                                [activity_id] => 1
                                [cnt] => 2
                            )
                        [1] => Array
                            (
                                [activity_id] => 2
                                [cnt] => 1
                            )
                        [2] => Array
                            (
                                [activity_id] => 3
                                [cnt] => 0
                            )
                    )
            )
        [1] => Array
            (
                [hour] => 11
                [activity] => Array
                    (
                        [0] => Array
                            (
                                [activity_id] => 1
                                [cnt] => 0
                            )
                        [1] => Array
                            (
                                [activity_id] => 2
                                [cnt] => 0
                            )
                        [2] => Array
                            (
                                [activity_id] => 3
                                [cnt] => 0
                            )
                    )
            )
        [2] => Array
            (
                [hour] => 12
                [percentage] => 0
                [activity] => Array
                    (
                        [0] => Array
                            (
                                [activity_id] => 1
                                [cnt] => 0
                            )
                        [1] => Array
                            (
                                [activity_id] => 2
                                [cnt] => 5
                            )
                        [2] => Array
                            (
                                [activity_id] => 3
                                [cnt] => 2
                            )
                    )
            )
    );

What i have tried is

$finalArray = array();
foreach($array1 as $arr1) {
    foreach($array2 as $arr2) {
        if(!in_array($arr2['id'], $arr1['activity'])) {
            $array = ['activity_id'=>$arr2['id'], 'cnt'=>0];   
        }
        array_push($arr1['activity'], $array);
        unset($array);
    }
    array_push($finalArray, $result);
}
print_r($finalArray);

in_array() function is not working as I excepted or I am trying to do it in the wrong way. Can someone helps me with this?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doutuosai3504
    doutuosai3504 2016-03-29 15:24
    已采纳

    Sorry,finally i get what i did wrong.May be someone get helped.

    everything is ok just change the line

    if(!in_array($arr2['id'], $arr1['activity'])) {
    

    into

    if(!in_array( $readActivity['id'], array_column($result['activity'],'activity_id'))){
    
    点赞 评论

相关推荐