php动态数组比较

时间:2017-02-02 03:34:45

标签: php arrays json

$arr1 = array(
       array('to' => '923336088811',    'country' => 'Pakistan'  ,   'operator' => 'Ufone (Pak Telecom)'     ,   'network_charges' => '0.5'        ,   'server' => '11'    ,     'methode' => 'JSON'       ,     'our_sms_id' => '235171135665'      ,     'our_sms_status_code' => '1'        ,     'action' => 'waiting'            ,     'message_status' => 'Pending'               ),
       array('to' => '923456812536',    'country' => 'Pakistan'  ,   'operator' => 'Ufone (Pak Telecom)'     ,   'network_charges' => '0.5'        ,   'server' => '11'    ,     'methode' => 'JSON'       ,     'our_sms_id' => '235171135665'      ,     'our_sms_status_code' => '1'        ,     'action' => 'waiting'            ,     'message_status' => 'Pending'               ),
       array('to' => '923008090100',    'country' => 'Pakistan'  ,   'operator' => 'Ufone (Pak Telecom)'     ,   'network_charges' => '0.5'        ,   'server' => '11'    ,     'methode' => 'JSON'       ,     'our_sms_id' => '235171135665'      ,     'our_sms_status_code' => '1'        ,     'action' => 'waiting'            ,     'message_status' => 'Pending'               ),
       );



$arr2 = array(
       array('to' => '923336088811',    'country' => 'Pakistan'  ,   'operator' => 'ZONG'     ,   'network_charges' => '10'        ,   'server' => '18'    ,     'methode' => 'JSON'       ,     'our_sms_id' => '235171135665'      ,     'our_sms_status_code' => '1'        ,     'action' => 'waiting'            ,     'message_status' => 'Pending'               ),
       array('to' => '923008090100',    'country' => 'Pakistan'  ,   'operator' => 'New'     ,   'network_charges' => '10'        ,   'server' => '18'    ,     'methode' => 'JSON'       ,     'our_sms_id' => '235171135665'      ,     'our_sms_status_code' => '1'        ,     'action' => 'waiting'             ,     'message_status' => 'Pending'               ),

        );


$ported_numbers = array_uintersect($arr2, $arr1, 'compareDeepValue');
$check = array_diff_key($arr1, $ported_numbers);
$result = array_merge($ported_numbers, $check);

print_r($result);




function compareDeepValue($val1, $val2){
return strcmp($val1['to'], $val2['to']);
}

想要将#arr1的项目替换为"至" 。由于以下两个项目与$ arr1匹配 '到' => ' 923336088811' '到' => ' 923008090100'

需要最终回复

Array
(
    [0] => Array
        (
            [to] => 923336088811
            [country] => Pakistan
            [operator] => ZONG
            [network_charges] => 10
            [server] => 18
            [methode] => JSON
            [our_sms_id] => 1
            [our_sms_status_code] => 4
            [action] => waiting
            [message_status] => Pending
        )

    [1] => Array
        (
            [to] => 923456812536
            [country] => Pakistan
            [operator] => Ufone (Pak Telecom)
            [network_charges] => 0.5
            [server] => 11
            [methode] => JSON
            [our_sms_id] => 2
            [our_sms_status_code] => 1
            [action] => waiting
            [message_status] => Pending
        )

    [2] => Array
        (
            [to] => 923008090100
            [country] => Pakistan
            [operator] => new
            [network_charges] => 10
            [server] => 18
            [methode] => JSON
            [our_sms_id] => 5
            [our_sms_status_code] => 1
            [action] => waiting
            [message_status] => Pending
        )

)

需要将$ arr1中的项目替换为"至"并且需要在不使用任何循环结构的情况下显示输出

1 个答案:

答案 0 :(得分:0)

你太近了。好消息是:只有一条线可以解决。 $check=array_udiff($arr1,$arr2,'compareDeepValue');

但是,我会完全细分您的代码,以便将来的读者可以看到array_uintersect()array_udiff()compareDeepValue()发生了什么。

function compareDeepValue($val1, $val2){
    return strcmp($val1['to'],$val2['to']);
}
$ported_numbers=array_uintersect($arr2,$arr1,'compareDeepValue');
/* Array(
    [0] => Array(
            [to] => 923336088811
            [country] => Pakistan
            [operator] => ZONG
            [network_charges] => 10
            [server] => 18
            [methode] => JSON
            [our_sms_id] => 235171135665
            [our_sms_status_code] => 1
            [action] => waiting
            [message_status] => Pending
        )
    [1] => Array(
            [to] => 923008090100
            [country] => Pakistan
            [operator] => New
            [network_charges] => 10
            [server] => 18
            [methode] => JSON
            [our_sms_id] => 235171135665
            [our_sms_status_code] => 1
            [action] => waiting
            [message_status] => Pending
        )
    ) */
// So far, so good.

// MalikIrfan's
// $check=array_diff_key($arr1,$ported_numbers);
/* Array(
    [2] => Array(
            [to] => 923008090100
            [country] => Pakistan
            [operator] => Ufone (Pak Telecom)
            [network_charges] => 0.5
            [server] => 11
            [methode] => JSON
            [our_sms_id] => 235171135665
            [our_sms_status_code] => 1
            [action] => waiting
            [message_status] => Pending
        )
    ) */
// this is not the desired collection of unmatched subarrays
// replace MalikIrfan's $check line with my $check line

$check=array_udiff($arr1,$arr2,'compareDeepValue');
// Returns array of unchanged ($arr1 only) subarrays
/* Array(
    [1] => Array
        (
            [to] => 923456812536
            [country] => Pakistan
            [operator] => Ufone (Pak Telecom)
            [network_charges] => 0.5
            [server] => 11
            [methode] => JSON
            [our_sms_id] => 235171135665
            [our_sms_status_code] => 1
            [action] => waiting
            [message_status] => Pending
        )
    ) */

$result=array_merge($ported_numbers,$check);
// The $result subarrays now ordered with changed rows first, unchanged second.
// This subarray order does not match the desired Final Response, but shouldn't matter.
// Also, I assume the our_sms_id values in Final Response were typos.
/* Array(
    [0] => Array(
            [to] => 923336088811
            [country] => Pakistan
            [operator] => ZONG
            [network_charges] => 10
            [server] => 18
            [methode] => JSON
            [our_sms_id] => 235171135665
            [our_sms_status_code] => 1
            [action] => waiting
            [message_status] => Pending
        ),
    [1] => Array(
            [to] => 923008090100
            [country] => Pakistan
            [operator] => New
            [network_charges] => 10
            [server] => 18
            [methode] => JSON
            [our_sms_id] => 235171135665
            [our_sms_status_code] => 1
            [action] => waiting
            [message_status] => Pending
        ),
    [2] => Array(
            [to] => 923456812536
            [country] => Pakistan
            [operator] => Ufone (Pak Telecom)
            [network_charges] => 0.5
            [server] => 11
            [methode] => JSON
            [our_sms_id] => 235171135665
            [our_sms_status_code] => 1
            [action] => waiting
            [message_status] => Pending
        )
    ) */