php – 比较多维数组并返回数组中缺少的键

我有两个多维关联数组,

首先我们有

Array
(
    [user_authentication] => Array
        (
            [api_user_id] => xxxxxxxxxxxxxxxxxxxxxxxx
            [api_auth_token] => xxxxxxxxxxxxxxxxxxxxxx
        )

    [campaign_details] => Array
        (
            [campaign_name] => democampaign
            [campaign_category] => appsGames
            [campaign_sub_category] => Action
            [campaign_type] => cpc
            [campaign_start_date] => MM/DD/YYYY
            [campaign_end_date] => MM/DD/YYYY
            [campaign_start_time] => HH:mm
            [campaign_end_time] => HH:mm
        )

    [campaign_budget_info] => Array
        (
            [campaign_daily_budget] => 0.2
            [campaign_hourly_budget] => 0.3
            [campaign_bid] => 0.1
            [campaign_budget] => 1
        )

    [campaign_targetting_info] => Array
        (
            [campaign_os_type] => Apple
            [country_code] => IN,AF,AG
            [state_id] => Array
                (
                    [IN] => 1,2,3
                    [AF] => 4,5,6
                    [AG] => 7,8,9
                )

            [carrier] => Array
                (
                    [IN] => Tata,Aircel,RCOM,Vodafone,Airtel,Idea Cellular,Uninor,Dishnet,BSNL
                    [AF] => 
                    [AG] => 
                )

            [isp] => 
            [device_targeting] => iphone,ipad
            [conversion] => 
        )

    [campaign_creative_info] => Array
        (
            [campaign_domain] => abcd.com
            [campaign_click_url] => http://url-to-redirect-users-to-after-they-click.com/
            [campaign_banner_size] => URL640x1136
            [campaign_banner_url] => http://imageurl.com/
            [campaign_creative_type] => image
        )

    [campaign_black_list_white_list_info] => Array
        (
            [black_list_app_ids] => 5388dceb96c4b54a0844e4cb,5330b3864dab485e6219ff54
            [black_list_device_ids] => aaaaaaaa-bbbb-cccc-0000-222222221111,12f93cf2-91ed-4f8f-aae7-a0520bebdd2r
            [black_list_ip_addresses] => 123.123.12.123,10.100.100.100
            [white_list_app_ids] => 5388dceb96c4b54a0844e4cb,5330b3864dab485e6219ff54
            [white_list_device_ids] => aaaaaaaa-bbbb-cccc-0000-222222221111,12f93cf2-91ed-4f8f-aae7-a0520bebdd2r
            [white_list_ip_addresses] => 123.123.12.123,10.100.100.100
        )

)

第二个是我要比较的

Array
(
    [user_authentication] => Array
        (
            [api_user_id] => 1
            [api_auth_token] => 1
        )

    [campaign_details] => Array
        (
            [campaign_name] => 1
            [campaign_category] => 1
            [campaign_sub_category] => 1
            [campaign_type] => 1
            [campaign_start_date] => 1
            [campaign_end_date] => 1
            [campaign_start_time] => 1
            [campaign_end_time] => 1
        )

    [campaign_budget_info] => Array
        (
            [campaign_daily_budget] => 1
            [campaign_hourly_budget] => 1
            [campaign_bid] => 1
            [campaign_budget] => 1
        )

    [campaign_targetting_info] => Array
        (
            [campaign_os_type] => 1
            [country_code] => 1
            [state_id] => Array
                (
                    [IN] => 1
                    [AF] => 1
                    [AG] => 1
                )

            [carrier] => Array
                (
                    [IN] => 1
                    [AF] => 1
                    [AG] => 1
                )

            [isp] => 1
            [device_targeting] => 1
            [conversion] => 1
        )

    [campaign_creative_info] => Array
        (
            [campaign_domain] => 1
            [campaign_click_url] => 1
            [campaign_banner_size] => 1
            [campaign_banner_url] => 1
            [campaign_creative_type] => 1
        )

    [campaign_black_list_white_list_info] => Array
        (
            [black_list_app_ids] => 1
            [black_list_device_ids] => 1
            [black_list_ip_addresses] => 1
            [white_list_app_ids] => 1
            [white_list_device_ids] => 1
            [white_list_ip_addresses] => 1
        )

)

我们必须比较数组并找到第一个数组中缺少的键

我试过这个但不行

$comparemodel= array_diff_assoc($array1,$array2); 

        if($comparemodel==0){
            echo "hello";
        }
        else{
            $keys = array_keys($comparemodel);
            for ($i = 0; $i < count($keys); $i++) {
                $error_message[] = $keys[$i] . " is missing";
            }
            $model = array();
            $errors = array("error_code" => 3042, "error_message" => $error_message);
            $message = $error_message;
            $status = 0;
            $finalarray = array("modal" => $model, "errors" => $errors, "message" => $message, "status" => $status);
            echo json_encode($finalarray);

        }

它不使用这个关联数组,但它使用简单的数组.我该怎么做呢

谢谢

最佳答案 试试这段代码

<?php
$arr1=array("campaign_details" => array
        (
        "campaign_name" => "democampaign",
        "campaign_category" => "appsGames",
        "campaign_sub_category" => "Action",
        "campaign_type" => "cpc",
        "campaign_start_date" => "MM/DD/YYYY",
        "campaign_end_date" => "MM/DD/YYYY",
        "campaign_start_time" => "HH:mm",
        "campaign_end_time" => "HH:mm"
        ),
        "campaign_budget_info" => array
        (
        "campaign_daily_budget" => 0.2,
        "campaign_hourly_budget" => 0.3,
        "campaign_bid" => 0.1,
        "campaign_budget" => 1,
        ),
        "campaign_targetting_info" => array
        (
        "campaign_os_type" => "Apple",
        "country_code" => "IN,AF,AG",
        "state_id" => array
        (
            "IN" => "1,2,3",
            "AF" => "4,5,6",
            "AG" => "7,8,9"
        ),
        "carrier" => Array
        (
            "IN" => "Tata,Aircel,RCOM,Vodafone,Airtel,Idea   
Cellular,Uninor,Dishnet,BSNL",
            "AF" => "",
            "AG" => "",
        ),

        "isp" => "",
        "device_targeting" => "iphone,ipad",
        "conversion" => "",
    ),
    "campaign_creative_info" => array
    (
        "campaign_domain" => "abcd.com",
        "campaign_click_url" => "http://url-to-redirect-users-to-after- 
they-click.com/",
        "campaign_banner_size" => "URL640x1136",
        "campaign_banner_url" => "http://imageurl.com/",
        "campaign_creative_type" => "image",
    ),
    "campaign_black_list_white_list_info" => array
    (
    "black_list_app_ids" => 
"5388dceb96c4b54a0844e4cb,5330b3864dab485e6219ff54",
    "black_list_device_ids" => "aaaaaaaa-bbbb-cccc-
0000-222222221111,12f93cf2-91ed-4f8f-aae7-a0520bebdd2r",
    "black_list_ip_addresses" => "123.123.12.123,10.100.100.100",
    "white_list_app_ids" =>  
"5388dceb96c4b54a0844e4cb,5330b3864dab485e6219ff54",
    "white_list_device_ids" => "aaaaaaaa-bbbb-cccc-
0000-222222221111,12f93cf2-91ed-4f8f-aae7-a0520bebdd2r",
    "white_list_ip_addresses" => "123.123.12.123,10.100.100.100",
    )
        );


$arr2=array("campaign_details" =>array
    (
        "campaign_name" => 1,
        "campaign_category" => 1,
        "campaign_sub_category" => 1,
        "campaign_type" => 1,
        "campaign_start_date" => 1,
        "campaign_end_date" => 1,
        "campaign_start_time" => 1,
        "campaign_end_time" => 1
    ),

    "campaign_budget_info" => array
    (
        "campaign_daily_budget" => 1,
        "campaign_hourly_budget" => 1,
        "campaign_bid" => 1,
        "campaign_budget" => 1,
    ),

    "campaign_targetting_info" => array
    (
        "campaign_os_type" => 1,
        "country_code" => 1,
        "state_id" => array
            (
                "IN" => 1,
                "AF" => 1,
                "AG" => 1,
            ),

        "carrier" => array
            (
                "IN" => 1,
                "AF" => 1,
                "AG" => 1,
            ),

        "isp" => 1,
        "device_targeting" => 1,
        "conversion" => 1,
    ),

    "campaign_creative_info" =>array
    (
        "campaign_domain" => 1,
        "campaign_click_url" => 1,
        "campaign_banner_size" => 1,
        "campaign_banner_url" => 1,
        "campaign_creative_type" => 1,
    ),

"campaign_black_list_white_list_info" => array
    (
        "black_list_app_ids" => 1,
        "black_list_device_ids" => 1,
        "black_list_ip_addresses" => 1,
        "white_list_app_ids" => 1,
        "white_list_device_ids" => 1,
        "white_list_ip_addresses" => 1,
    )            

      );
function array_keys_multi(array $array)
{
$keys = array();

foreach ($array as $key => $value) {
    $keys[] = $key;

    if (is_array($array[$key])) {
        $keys = array_merge($keys, array_keys_multi($array[$key]));
     }
 }

 return $keys;
}

$resArr=array();
$a=array_keys_multi($arr1);
$b=array_keys_multi($arr2);
$c=array_diff($a,$b);
if(count($c) > 0){
echo  "There is differnce<br/>";
echo "<pre>";
print_r($c);
}else
echo  "There is no differnce<br/>";
?>
点赞