# Array comparison Efficiency

Source: Internet
Author: User
Two Arrays: ArrayA and ArrayB {code ...} {code ...} expected result {code ...} my current practice is to do a two-tier loop {code ...} is there any more efficient way? Two Arrays: ArrayA and 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,  ),);
\$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',  ),);

Expected results

\$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',  ),);

My current practice is to create a two-layer Loop

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

Is there any more efficient way?

Two Arrays: ArrayA and 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,  ),);
\$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',  ),);

Expected results

\$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',  ),);

My current practice is to create a two-layer Loop

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

Is there any more efficient way?

Use PHP built-in functionsarray_intersectYou can.

Reference

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.