[英]How to create new array of dates from existing arrays of dates in PHP?
I have 2 arrays of dates. 我有2个日期数组。 I need the most efficient way to build a third array of dates that consists of only the dates that are the same from the 2 arrays of dates.
我需要最有效的方法来构建第三个日期数组,该数组仅包含与2个日期数组相同的日期。
For example: 例如:
List of Dates #1:
2017-01-01
2017-01-02
2017-01-03
2017-02-01
2017-02-02
2017-09-09
List of Dates #2:
2017-01-01
2017-02-01
2017-03-01
2017-04-01
So array 3 should be:
2017-01-01
2017-02-01
You are looking for array_intersect : 您正在寻找array_intersect :
Working Demo : https://eval.in/862254 工作演示 : https : //eval.in/862254
$array1 = array("2017-01-01", "2017-01-01" ,"2017-01-02","2017-01-03","2017-02-01", "2017-02-02","2017-09-09");
$array2 = array("2017-01-01","2017-02-01","2017-03-01","2017-04-01");
$result = array_intersect(array_unique($array1), $array2);
echo "<pre>";
print_r($result);
Output: 输出:
Array
(
[0] => 2017-01-01
[3] => 2017-02-01
)
Note 注意
array_intersect
Returns an array containing all of the values in$array1
whose values exist in all of the parameters.array_intersect
返回一个数组,其中包含$array1
中的所有值,其值存在于所有参数中。 So if your$array1
is having duplicate data then resultant array also have duplicate values.因此,如果您的
$array1
具有重复数据,那么结果数组也将具有重复值。
I have added array_unique to overcome that situation. 我添加了array_unique来克服这种情况。
Maybe array_intersect() ? 也许array_intersect() ?
$array1 = array ("a" => "green", "red", "blue");
$array2 = array ("b" => "green", "yellow", "red");
$result = array_intersect ($array1, $array2);
result 结果
Array
(
[a] => green
[0] => red
)
Try this 尝试这个
function myfunction($a,$b)
{
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"red","b"=>"green","c"=>"blue","yellow");
$a2=array("A"=>"red","b"=>"GREEN","yellow","black");
$a3=array("a"=>"green","b"=>"red","yellow","black");
$result=array_uintersect($a1,$a2,$a3,"myfunction");
print_r($result);
Result: 结果:
Array ( [a] => red [0] => yellow )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.