PHP increases function efficiency with two associative array merge functions

Source: Internet
Author: User
Tags foreach arrays

  Loops query data in foreach with less code, but performance is low, and functions written using the following bytes can resolve

In foreach, the number of circular query data is less, but the performance is relatively low, the better solution is to collect the ID, in a one-time query, but this caused the data structure is not we use PHP with the functions can be merged, today tested a:    Using the following byte write function to solve     data extracted from the database is always more or less inconsistent with our idea of the data structure, similar to the following two arrays, to form a SQL similar to the left join after the two array merge:  code is as follows: $ Test1 = Array (  0 => Array (  ' id ' => 9478137,  ' create_time ' => 1394760724 ),  1 => Array (  ' id ' => 9478138,  ' create_time ' => 1394760725 ),  2 => Array (  ' id ' => 94781 38,  ' Create_time ' => 1394760725 )  ;  $test 2 = Array (  0 => Array (  ' ID ' => 9478 137,  ' => ' Love You '  ),  1 => Array (  ' id ' => 9478138,  ' message ' => ' Miss You '  )  ;    If you want to associate these two arrays, like a left join in SQL, what function do we use? Well, I didn't find it.   started with nested loops: Inefficient     Code as follows: function _mergerarray ($array 1, $array 2, $field 1, $field 2 = ') {   $ret = Array ();  foreach ($array 1 as $key 1 => $value 1) {  foreach ($array 2 as $key 2 => $value 2) {  if ($value 1[$field 1] = = $value 2[$field 2]) {  $ret [$key 1] = Array_merge ($value 1, $value 2) ; } } }  return $ret; }    Improved method, using array subscript, use two cycles: form a way like left join   code as follows: $tes T1 = Array (  0 => Array (  ' id ' => 9478137,  ' create_time ' => 1394760724 ),  1 => Arr Ay (  ' id ' => 9478138,  ' create_time ' => 1394760725 ),  2 => Array (  ' id ' => 9478138,   ' Create_time ' => 1394760725 )  ;  $test 2 = Array (  0 => Array (  ' ID ' => 9478137 ,  ' => ' Love You '  ,  1 => Array (  ' id ' => 9478138,  ' message ' => ' Miss '  )  ;    function _mergerarray ($array 1, $array 2, $field 1, $field 2 = ') {  $ret = Array (); &nbs P  //Use array subscript method   foreach ($array 2 as $key => $value) {  $array 3[$value [$field 1]] = $value; }  F Oreach ($array 1 as $keY => $value) {  $ret [] = Array_merge ($array 3[$value [$field 1]], $value); }  return $ret; }  $ ret = _mergerarray ($test 1, $test 2, ' id ', ' id ');  print_r ($ret);exit;    Print the results are as follows:    code as follows: Array   (  [0] => array  (  [id] => 9478137  [message] => love you  [Create_time] => 1 394760724 )   [1] => array  (  [id] => 9478138  [message] => Miss you  [Create_time ] => 1394760725 )   [2] => array  (  [id] => 9478138  [message] => Miss you  [cr Eate_time] => 1394760725   )     is equivalent to left join?  

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.