Home > Backend Development > PHP Tutorial > 数组对比效率问题

数组对比效率问题

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:51:09
Original
993 people have browsed it

两个数组,ArrayA和ArrayB

$arrayA = array (
  0 => array (
    0 => 1,
    4 => 1,
    6 => 1,
  ),
  1 => array (
    1 => 1,
    3 => 1,
    5 => 1,
  ),
  2 => array (
    7 => 1,
    8 => 1,
    9 => 1,
  ),
);
Copy after login
Copy after login
$arrayB = array (
  0 => array (
    0 => 'a',
    1 => 'b',
    2 => 'c',
    3 => 'd',
    4 => 'e',
    5 => 'f',
    6 => 'g',
    7 => 'h',
    8 => 'i',
    9 => 'j',
  ),
  1 => array (
    0 => 'a',
    1 => 'b',
    2 => 'c',
    3 => 'd',
    4 => 'e',
    5 => 'f',
    6 => 'g',
    7 => 'h',
    8 => 'i',
    9 => 'j',
  ),
  2 => array (
    0 => 'a',
    1 => 'b',
    2 => 'c',
    3 => 'd',
    4 => 'e',
    5 => 'f',
    6 => 'g',
    7 => 'h',
    8 => 'i',
    9 => 'j',
  ),
);
Copy after login
Copy after login

希望得到结果

$result = array (
  0 => array (
    0 => 'a',
    4 => 'e',
    6 => 'g',
  ),
  1 => array (
    1 => 'b',
    3 => 'd',
    5 => 'f',
  ),
  2 => array (
    7 => 'h',
    8 => 'i',
    9 => 'j',
  ),
);
Copy after login
Copy after login

我现在的做法是做两层循环

$result = array ();
foreach ($arrayA as $k => $v) {
  foreach ($v as $kk => $vv) {
    $result[$k][$kk] = $arrayB[$k][$kk];
  }
}
Copy after login
Copy after login

有没有什么效率更高的方式呢?

回复内容:

两个数组,ArrayA和ArrayB

$arrayA = array (
  0 => array (
    0 => 1,
    4 => 1,
    6 => 1,
  ),
  1 => array (
    1 => 1,
    3 => 1,
    5 => 1,
  ),
  2 => array (
    7 => 1,
    8 => 1,
    9 => 1,
  ),
);
Copy after login
Copy after login
$arrayB = array (
  0 => array (
    0 => 'a',
    1 => 'b',
    2 => 'c',
    3 => 'd',
    4 => 'e',
    5 => 'f',
    6 => 'g',
    7 => 'h',
    8 => 'i',
    9 => 'j',
  ),
  1 => array (
    0 => 'a',
    1 => 'b',
    2 => 'c',
    3 => 'd',
    4 => 'e',
    5 => 'f',
    6 => 'g',
    7 => 'h',
    8 => 'i',
    9 => 'j',
  ),
  2 => array (
    0 => 'a',
    1 => 'b',
    2 => 'c',
    3 => 'd',
    4 => 'e',
    5 => 'f',
    6 => 'g',
    7 => 'h',
    8 => 'i',
    9 => 'j',
  ),
);
Copy after login
Copy after login

希望得到结果

$result = array (
  0 => array (
    0 => 'a',
    4 => 'e',
    6 => 'g',
  ),
  1 => array (
    1 => 'b',
    3 => 'd',
    5 => 'f',
  ),
  2 => array (
    7 => 'h',
    8 => 'i',
    9 => 'j',
  ),
);
Copy after login
Copy after login

我现在的做法是做两层循环

$result = array ();
foreach ($arrayA as $k => $v) {
  foreach ($v as $kk => $vv) {
    $result[$k][$kk] = $arrayB[$k][$kk];
  }
}
Copy after login
Copy after login

有没有什么效率更高的方式呢?

使用 PHP 的内置函数 array_intersect 即可

参考

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template