比较两个数组 - 返回不匹配的数组项

时间:2017-01-24 00:01:57

标签: php arrays laravel multidimensional-array associative-array

我有2个数组,如下所示。

$ ARR1 =

array:8 [▼
  0 => array:18 [▼

    "c_id" => 19
    "datetime" => "2017-01-20 21:48:16"
    "aparty" => "8801911255104"
    "bparty" => "8801961185321"
    "call_duration" => "0"
    "usage_type" => "SMSMT"
    "lac" => "532"
    "cell" => "13174"
    "imei" => "357328068823480"
    "imsi" => "470037500180672"
    "network_type" => "Undefined"
  ]
  1 => array:18 [▶]
  2 => array:18 [▶]
  3 => array:18 [▶]
  4 => array:18 [▶]
  5 => array:18 [▶]
  6 => array:18 [▶]
  7 => array:18 [▶]
]


$arr2= = [
          'c_id' => '1',
          'datetime' =>'2017-01-20 21:48:16' ,
          'aparty' => '8801911255104',
          'bparty' =>'8801911255104',
          'call_duration' => '999',
          'usage_type' =>'MC',
          'lac' => '555',
          'cell' => '99',
          'imei' => '6y8887766',
          'imsi' => '6y8887766',
          'network_type' =>'Undefined'
  ];

现在我想将第二个数组与第一个数组进行比较。逻辑是如果$ arr1中不存在$arr2,则插入到database.if exists然后没有插入

我无法理解,我正在尝试使用此代码但无法正常工作

foreach($arr1 as $k=>$v){
    $dif = abs($arr1[$k] - $arr2[$k]);
    if($dif) return false else return true;
}

1 个答案:

答案 0 :(得分:0)

您已将问题标记为laravel并且这是关于插入数据库的,因此也假设为Eqloeunt。

您可以利用以下Eloquent功能:

$model = App\YourModel::firstOrCreate($arr2);

可以直接节省数组乱码。

参考:https://laravel.com/docs/master/eloquent#inserts