检查数组值是否存在于另一个数组中,如果不存在,则替换为另一个数组

时间:2017-06-01 03:47:05

标签: php arrays compare

我这里有两个数组。我使用json_encode()

回应了它
$myarray1= [{"date":"2017-04-18","count":"3"},{"date":"2017-04-19","count":"2"},{"date":"2017-04-20","count":"1"},{"date":"2017-04-24","count":"1"},{"date":"2017-04-25","count":"1"},{"date":"2017-05-02","count":"2"},{"date":"2017-05-03","count":"1"},{"date":"2017-05-04","count":"1"},{"date":"2017-05-09","count":"1"},{"date":"2017-05-10","count":"2"},{"date":"2017-05-11","count":"6"},{"date":"2017-05-12","count":"5"},{"date":"2017-05-17","count":"3"},{"date":"2017-05-19","count":"2"},{"date":"2017-05-30","count":"3"}]

$myarray2= [{"date":"2017-04-18"},{"date":"2017-04-19"},{"date":"2017-04-20"},{"date":"2017-04-21"},{"date":"2017-04-22"},{"date":"2017-04-23"},{"date":"2017-04-24"},{"date":"2017-04-25"},{"date":"2017-04-26"},{"date":"2017-04-27"},{"date":"2017-04-28"},{"date":"2017-04-29"},{"date":"2017-04-30"},{"date":"2017-05-01"},{"date":"2017-05-02"},{"date":"2017-05-03"},{"date":"2017-05-04"},{"date":"2017-05-05"},{"date":"2017-05-06"},{"date":"2017-05-07"},{"date":"2017-05-08"},{"date":"2017-05-09"},{"date":"2017-05-10"},{"date":"2017-05-11"},{"date":"2017-05-12"},{"date":"2017-05-13"},{"date":"2017-05-14"},{"date":"2017-05-15"},{"date":"2017-05-16"},{"date":"2017-05-17"},{"date":"2017-05-18"},{"date":"2017-05-19"},{"date":"2017-05-20"},{"date":"2017-05-21"},{"date":"2017-05-22"},{"date":"2017-05-23"},{"date":"2017-05-24"},{"date":"2017-05-25"},{"date":"2017-05-26"},{"date":"2017-05-27"},{"date":"2017-05-28"},{"date":"2017-05-29"},{"date":"2017-05-30"},{"date":"2017-05-31"},{"date":"2017-06-01"}]

如何检查$myarray2数组中是否有$myarray1值?

谢谢大家。

3 个答案:

答案 0 :(得分:1)

您可以使用array_intersect

示例:

<?php
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
print_r($result);
?>

了解更多文档 http://php.net/manual/en/function.array-intersect.php

答案 1 :(得分:1)

我们正在使用json_decode array_columnarray_intersect

Try this code snippet here

<?php
ini_set('display_errors', 1);

$string1='[{"date":"2017-04-18","count":"3"},{"date":"2017-04-19","count":"2"},{"date":"2017-04-20","count":"1"},{"date":"2017-04-24","count":"1"},{"date":"2017-04-25","count":"1"},{"date":"2017-05-02","count":"2"},{"date":"2017-05-03","count":"1"},{"date":"2017-05-04","count":"1"},{"date":"2017-05-09","count":"1"},{"date":"2017-05-10","count":"2"},{"date":"2017-05-11","count":"6"},{"date":"2017-05-12","count":"5"},{"date":"2017-05-17","count":"3"},{"date":"2017-05-19","count":"2"},{"date":"2017-05-30","count":"3"}]';
$string2='[{"date":"2017-04-18"},{"date":"2017-04-19"},{"date":"2017-04-20"},{"date":"2017-04-21"},{"date":"2017-04-22"},{"date":"2017-04-23"},{"date":"2017-04-24"},{"date":"2017-04-25"},{"date":"2017-04-26"},{"date":"2017-04-27"},{"date":"2017-04-28"},{"date":"2017-04-29"},{"date":"2017-04-30"},{"date":"2017-05-01"},{"date":"2017-05-02"},{"date":"2017-05-03"},{"date":"2017-05-04"},{"date":"2017-05-05"},{"date":"2017-05-06"},{"date":"2017-05-07"},{"date":"2017-05-08"},{"date":"2017-05-09"},{"date":"2017-05-10"},{"date":"2017-05-11"},{"date":"2017-05-12"},{"date":"2017-05-13"},{"date":"2017-05-14"},{"date":"2017-05-15"},{"date":"2017-05-16"},{"date":"2017-05-17"},{"date":"2017-05-18"},{"date":"2017-05-19"},{"date":"2017-05-20"},{"date":"2017-05-21"},{"date":"2017-05-22"},{"date":"2017-05-23"},{"date":"2017-05-24"},{"date":"2017-05-25"},{"date":"2017-05-26"},{"date":"2017-05-27"},{"date":"2017-05-28"},{"date":"2017-05-29"},{"date":"2017-05-30"},{"date":"2017-05-31"},{"date":"2017-06-01"}]';

$array1=json_decode($string1,true);
$array2=json_decode($string2,true);

print_r(array_intersect(array_column($array1, "date"), array_column($array2,"date")));

答案 2 :(得分:1)

您不希望intersect(),想要diff()

Demo

var_export(array_diff(array_column($array2,'date'),array_column($array1,'date')));

输出(不匹配的日期):

array (
  3 => '2017-04-21',
  4 => '2017-04-22',
  5 => '2017-04-23',
  8 => '2017-04-26',
  9 => '2017-04-27',
  10 => '2017-04-28',
  11 => '2017-04-29',
  12 => '2017-04-30',
  13 => '2017-05-01',
  17 => '2017-05-05',
  18 => '2017-05-06',
  19 => '2017-05-07',
  20 => '2017-05-08',
  25 => '2017-05-13',
  26 => '2017-05-14',
  27 => '2017-05-15',
  28 => '2017-05-16',
  30 => '2017-05-18',
  32 => '2017-05-20',
  33 => '2017-05-21',
  34 => '2017-05-22',
  35 => '2017-05-23',
  36 => '2017-05-24',
  37 => '2017-05-25',
  38 => '2017-05-26',
  39 => '2017-05-27',
  40 => '2017-05-28',
  41 => '2017-05-29',
  43 => '2017-05-31',
  44 => '2017-06-01',
)