php数组比较索引按索引 [英] php array comparison index by index

查看:161
本文介绍了php数组比较索引按索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有两个数组变量包含完全相同的数字(无重复),但是在位置上混乱。

If there are two array variable which contains exact same digits(no duplicate) but shuffled in position.

输入数组 >

Input arrays

arr1={1,4,6,7,8};
arr2={1,7,7,6,8};

结果数组

arr2={true,false,false,false,true};

在php中有一个函数用于获取上述结果,或者应该使用loop


Is there a function in php to get result as above or should it be done using loop(which I can do) only.

推荐答案

这是一个不错的应用程序 array_map() 和一个匿名回调(OK,我必须承认我喜欢那些闭包; - )

This is a nice application for array_map() and an anonymous callback (OK, I must admit that I like those closures ;-)

$a1 = array(1,4,6,7,8);
$a2 = array(1,7,7,6,8);

$r = array_map(function($a1, $a2) {
    return $a1 === $a2;
}, $a1, $a2);

var_dump($r);

/*
array(5) {
  [0]=>
  bool(true)
  [1]=>
  bool(false)
  [2]=>
  bool(false)
  [3]=>
  bool(false)
  [4]=>
  bool(true)
}
*/

,你必须以某种方式循环数组。

And yes, you have to loop over the array some way or the other.

这篇关于php数组比较索引按索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆