Several methods of duplicate names in PHP output array

Source: Internet
Author: User
For you to introduce several PHP output array of the same name elements of the method, the need for friends, you can refer to the next.

Method 1, directly with PHP built-in function array_intersect () array array_intersect (array $array 1, array $array 2 [, array $ ...]) Array_intersect () Returns an array that contains all the values in Array1 that also appear in all other parameter arrays. Note The key name remains unchanged. Example:

 
      "Green", "Red", "Blue");      $array 2 = Array ("b" = "green", "yellow", "red");      $result = Array_intersect ($array 1, $array 2);  ? >

Output: Array ([a] = green [0] = + red)

Method 2, self-implemented algorithm

 
      ";              $k =var_dump (Array_unique ($r));              return $k;                    }      $a = Array ("Red", "green", "pink",  "red", "yellow", "pink", "red");      $r =my_array_same ($a);      Var_dump (Array_unique ($r));  ? >

Output: Array (2) {[3]=> string (3) "Red" [5]=> string (4) "Pink"}

3. Custom Recursive functions

 
      ";      Print_r (My_array_intersect ($array 1, $array 2));      echo "
";  ? >

Description: In one-dimensional arrays, the third algorithm is faster than the first type. All of these algorithms apply to one-dimensional arrays, so how do multidimensional arrays find the same elements? Here is a way of thinking: You can convert multidimensional arrays into one-dimensional arrays, and then use the above algorithm output. Example:

 
      $v) {               if (!is_array ($v)) {                  $str. = $v. " ";               }               else{                  $str. =toarr ($v);               }          }           return $str;      } /* Recursive function End */  ?>

The above code implements a multidimensional array into a string, which is then converted into a one-dimensional array using the Expode function. Imagine that a database returns a value with the same name as a field, which is also possible with SQL statements.

Programming is like this, all the way to Rome, a return to three, touch and bypass, more learning more practice, charm ah. The programmer's home, wish everybody to study the progress.

  • 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.