I have two arrays that i want to compare, and then collect the similar values to use in order to display values from a third array.
array 1:
我有两个我想比较的数组,然后收集类似的值来显示第三个数组的值。数组1:
$Global_Days = array("Monday", "Thursday", "Friday", "Sunday");
array 2:
$Global_Day = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
array 3:
$Global_Dates = array("11-05-2015", "12-05-2015", "13-05-2015", "14-05-2015", "15-05-2015", "16-05-2015", "17-05-2015");
So what i basically want is for it to display the date equal to the days shown in the first array, so this example would show:
所以我基本上想要的是它显示的日期等于第一个数组中显示的日期,所以这个例子会显示:
11-05-2015
14-05-2015
15-05-2015
17-05-2015
From what I have seen, array_intersect() is roughly what I am looking for, but examples that I have seen of it are very confusing.
the data shown here is a bit different to what I intend to use, but this functionality is what I need, so giving alternatives to getting the date won't help
从我所看到的,array_intersect()大致是我正在寻找的,但我看到它的例子非常令人困惑。这里显示的数据与我打算使用的有点不同,但这个功能是我需要的,所以给出获取日期的替代方案将无济于事
2 个解决方案
#1
Things to note. When intersecting you need to pay attention to your first array, the one to be compared against. array_intersect provides you all of the values in $Global_Day at the indices mapped to $Global_Dates. You can then map the result of this intersection with with $Global_Dates to retrieve the mapped values by comparing the keys retrieved from the first intersection.
注意事项。相交时,您需要注意第一个数组,即要比较的数组。 array_intersect为映射到$ Global_Dates的索引提供$ Global_Day中的所有值。然后,您可以使用$ Global_Dates映射此交集的结果,以通过比较从第一个交叉点检索的键来检索映射的值。
This should achieve your goal
这应该达到你的目标
$Global_Days = array("Monday", "Thursday", "Friday", "Sunday");
$Global_Day = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
$Global_Dates = array("11-05-2015", "12-05-2015", "13-05-2015", "14-05-2015", "15-05-2015", "16-05-2015", "17-05-2015");
$keys = array_intersect($Global_Day, $Global_Days);
$result = array_intersect_key($Global_Dates, $keys);
Try it out: http://www.tehplayground.com/#zkyzV0dwH
试一试:http://www.tehplayground.com/#zkyzV0dwH
#2
Well, heres what i ended up using, might not be optimal, but it gets the values, tried to code based on Barmars answer and ended up with this:
嗯,继续我最终使用的,可能不是最优的,但它获得了值,试图根据Barmars的答案进行编码,最后得到了这样的结果:
$ans = array_intersect($Global_Days, $Global_Day);
$AKans = array_keys($ans);
for ($z=0; $z < count($AKans); $z++ ){
$TAK = $AKans[$z];
echo $Global_Dates[$TAK];
echo "</br>";
}
#1
Things to note. When intersecting you need to pay attention to your first array, the one to be compared against. array_intersect provides you all of the values in $Global_Day at the indices mapped to $Global_Dates. You can then map the result of this intersection with with $Global_Dates to retrieve the mapped values by comparing the keys retrieved from the first intersection.
注意事项。相交时,您需要注意第一个数组,即要比较的数组。 array_intersect为映射到$ Global_Dates的索引提供$ Global_Day中的所有值。然后,您可以使用$ Global_Dates映射此交集的结果,以通过比较从第一个交叉点检索的键来检索映射的值。
This should achieve your goal
这应该达到你的目标
$Global_Days = array("Monday", "Thursday", "Friday", "Sunday");
$Global_Day = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
$Global_Dates = array("11-05-2015", "12-05-2015", "13-05-2015", "14-05-2015", "15-05-2015", "16-05-2015", "17-05-2015");
$keys = array_intersect($Global_Day, $Global_Days);
$result = array_intersect_key($Global_Dates, $keys);
Try it out: http://www.tehplayground.com/#zkyzV0dwH
试一试:http://www.tehplayground.com/#zkyzV0dwH
#2
Well, heres what i ended up using, might not be optimal, but it gets the values, tried to code based on Barmars answer and ended up with this:
嗯,继续我最终使用的,可能不是最优的,但它获得了值,试图根据Barmars的答案进行编码,最后得到了这样的结果:
$ans = array_intersect($Global_Days, $Global_Day);
$AKans = array_keys($ans);
for ($z=0; $z < count($AKans); $z++ ){
$TAK = $AKans[$z];
echo $Global_Dates[$TAK];
echo "</br>";
}