Home > Backend Development > PHP Tutorial > Several methods for php to output elements with duplicate names in arrays

Several methods for php to output elements with duplicate names in arrays

WBOY
Release: 2016-07-25 08:59:50
Original
1309 people have browsed it
I will introduce to you several methods of PHP outputting elements with the same name in an array. Friends in need can refer to them.

Method 1. Directly use PHP’s built-in function array_intersect() array array_intersect ( array $array1 , array $array2 [, array $ ... ] ) array_intersect() returns an array containing all values ​​in array1 that are also present in all other argument arrays. Note that the key names remain unchanged. Example:

<?php  
    $array1 = array("a" => "green", "red", "blue");  
    $array2 = array("b" => "green", "yellow", "red");  
    $result = array_intersect($array1, $array2);  
?>
Copy after login

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

Method 2, self-implemented algorithm

<?php  
    function my_array_same($a){  
            $b = array_unique($a);  
            $r = array_diff_key($a,$b);  
            echo "<pre class="brush:php;toolbar:false">";  
            $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));  
?>
Copy after login

Output result: array(2) { [3]=> string(3) "red" [5]=> string(4) "pink" }

3. Custom recursive function

<?php  
    function my_array_intersect($arr1,$arr2){      
        for($i=0;$i<count($arr1);$i++){          
            $temp[]=$arr1[$i];      
            }   
        for($i=0;$i<count($arr1);$i++){          
            $temp[]=$arr2[$i];      
            }   
         sort($temp);   
         $get=array();          
         for($i=0;$i<count($temp);$i++){          
            if($temp[$i]==$temp[$i+1])           
               $get[]=$temp[$i];      
             }          
         return $get;  
      }  
    $array1 = array("green", "red", "blue");  
    $array2 = array("green", "yellow", "red");  
    echo "<pre class="brush:php;toolbar:false">";  
    print_r(my_array_intersect($array1, $array2));  
    echo "<pre/>";  
?>
Copy after login

Instructions: For one-dimensional arrays, the third algorithm is faster than the first. The above algorithms are all applicable to one-dimensional arrays, so how to find the same elements in multi-dimensional arrays? Here is an idea: you can convert a multi-dimensional array into a one-dimensional array, and then use the above algorithm to output it. Example:

<?php
    function toarr($arr){ //对数组进行递归,以字符串形式返回  
        foreach ($arr as $k=>$v){  
             if (!is_array($v)) {  
                $str.=$v." ";  
             }  
             else{  
                $str.=toarr($v);  
             }  
        }     
      return $str;  
    }/*递归函数结束*/  
?>
Copy after login

The above code converts a multi-dimensional array into a string, and then uses the expode function to convert it into a one-dimensional array. Just imagine, this is also the case when the database returns the value of a certain field with the same name. This can also be achieved through SQL statements.

Programming is like this. All roads lead to Rome. Apply one example to another, draw analogies and draw parallels. Learn more, practice more, practice more, and success will come naturally. Programmer's Home, I wish you all the best in your studies and progress.



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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template