2017-02-02 11 views
0
$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']); 
} 

"to"に対して$ arr1の項目を置き換えたい。 2として、以下の項目がPHPダイナミックアレイの比較

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でアイテムを交換する必要があるとして

が最終回答が必要=> '923336088811' => '923008090100' 'に' 'に' $のARR1 と一致していますキーを "to"と見なし、ループ構造を使用せずに出力を表示する必要があります。

+0

ループを使用できないのはなぜですか? –

+0

ループ構造はサーバーのより多くのリソースを消費します。 –

+0

膨大な量のデータを処理していない、またはこれを頻繁に実行しない限り、ループを使用しないと、サーバー上の負荷よりもはるかに多くの負荷がかかることがあります。 –

答えて

0

あなたはとても近くでした。良いニュースは、修正する行は1つだけです。 $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 
     ) 
    ) */ 
+0

@MalikIrfan私の答えがあなたの問題を解決するなら、それを緑のダニと助けてください(おそらくupvote)を与えてください。そうでない場合は、まだ問題があるかどうかを説明してください。 – mickmackusa