简体   繁体   中英

Compare two associative array and combine both data in php

Unable to get difference associative array with combined data. Those value which is not found in array:4 should be combined with array:2 as like below with count 0:

2=>array:2[
"value" => Occupation
"count" => 0
]

What I have already tried

$datas=[];
        foreach($data as $arrayIndex=>$element){
            $match = false;
            foreach($domains as $key=>$elementToMatch){
                if($element['value'] != $elementToMatch ){
                    $match = true;
                    $counts = ['value'=>$elementToMatch,'count'=>0];
                }
                if($match == true) {
                    break;
                }

            }

            if($match) {
                array_push($datas,$counts);
            }
        }

Please help在此处输入图片说明

$values = array_column($array2, 'value');
foreach ($array4 as $item) {
    if ( ! in_array($item, $values, true)) {
        $array2[] = [
            'value' => $item,
            'count' => 0
        ];
    }
}

I just used array_column to get the value of all the value of the array:2 and check if all the value from array:4 are present in that data using in_array

If not present add the missing value with count 0 to array:2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM