Array comparison efficiency problem

Source: Internet
Author: User
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 ',  ),);

Hope to get 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 ',  ),);

The way I do it now is to do two layers of loops

$result = Array (), foreach ($arrayA as $k = = $v) {  foreach ($v as $kk + = $VV) {    $result [$k] [$KK] = $arrayB [$k] [$KK];  }}

Is there any more efficient way?

Reply content:

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

Hope to get results

$result = Array (  0 = = Array (    0 = ' A ',    4 = ' E ',    6 = ' g ',  ),  1 = = Array ( c42/>1 = ' B ',    3 = ' d ',    5 = ' f ',  ),  2 = = Array (    7 = ' h ',    8 = ' I ', 
  9 = ' J ',  ),);

The way I do it now is to do two layers of loops

$result = Array (), foreach ($arrayA as $k = = $v) {  foreach ($v as $kk + = $VV) {    $result [$k] [$KK] = $arrayB [$ k][$KK];  }}

Is there any more efficient way?

You can use the built-in functions of PHP array_intersect

Reference

  • Contact Us

    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.