php array

模拟数据结构

数组后添加
array_push
$a=array("red","green");
array_push($a,"blue","yellow");
Array ( [0] => red [1] => green [2] => blue [3] => yellow ) 

数组前添加
array_unshift
$a=array("a"=>"red","b"=>"green");
array_unshift($a,"blue");
Array ( [0] => blue [a] => red [b] => green ) 

数组前删除
$a=array("a"=>"red","b"=>"green","c"=>"blue");
echo array_shift($a);
redArray ( [b] => green [c] => blue ) 

数组后删除
$a=array("red","green","blue"); is a common problem for sorting two-dimensional arrays or multi-dimensional arrays. In PHP, we have a special The multidimensional array sorting function of , is briefly introduced below: PHP Basics - Sorting Two-Dimensional Arrays array_multisort  Array ( [0] => red [1] => green ) 
array_pop($a);





array_multisort(array1, sorting order, sorting type, array2, array3..) is a function to sort multiple arrays or multidimensional arrays.
array1 is required. Specifies an array of inputs.
sorting order is optional. Specifies the sort order. Possible values ​​are SORT_ASC and SORT_DESC.
sorting type is optional. Specifies the sort type. Possible values ​​are SORT_REGULAR, SORT_NUMERIC, and SORT_STRING.
array2 is optional. Specifies an array of inputs.
array3 is optional. Specifies an array of inputs.
The array in the parameter is treated as a table column and sorted by row - this is similar to how SQL's ORDER BY clause functions. The first array is the main array to be sorted. Rows (values) in an array that compare equal

are sorted by the size of the corresponding value in the next input array, and so on.
The first argument is an array, each subsequent argument may be an array, or one of the following sort order flags (sort flags are used to change the default sort order):
    SORT_ASC - Default, ascending order. (AZ)
    SORT_DESC - Sort in descending order. (ZA)
The type of sorting can then be specified:
    SORT_REGULAR - default. Arrange each item in the usual order.
    SORT_NUMERIC - put each item in numerical order.
    SORT_STRING - 将每一项按字母顺序排列

    <?php  
        function my_sort($arrays,$sort_key,$sort_order=SORT_ASC,$sort_type=SORT_NUMERIC ){   
            if(is_array($arrays)){   
                foreach ($arrays as $array){   
                    if(is_array($array)){   
                        $key_arrays[] = $array[$sort_key];   
                    }else{   
                        return false;   
                    }   
                }   
            }else{   
                return false;   
            }  
            array_multisort($key_arrays,$sort_order,$sort_type,$arrays);   
            return $arrays;   
        }  
        $person =  array(  
                        array('id'=>1,'name'=>'fj','weight'=>100,'height'=>180),  
                        array('id'=>2,'name'=>'tom','weight'=>53,'height'=>150),  
                        array('id'=>3,'name'=>'jerry','weight'=>120,'height'=>156),  
                        array('id'=>4,'name'=>'bill','weight'=>110,'height'=>190),  
                        array('id'=>5,'name'=>'linken','weight'=>80,'height'=>200),  
                        array('id'=>6,'name'=>'madana','weight'=>95,'height'=>110),  
                        array('id'=>7,'name'=>'jordan','weight'=>70,'height'=>170)  
                    );  
        var_dump($person);  
        $person = my_sort($person,'name',SORT_ASC,SORT_STRING);  
        var_dump($person);   
        $person = my_sort($person,'weight');  
        var_dump($person);  
    ?>  

For example an array like the following: The
code is as follows:

$users = array(
  array('name' => 'tom', 'age' => 20)
  , array('name' => 'anny', 'age' => 18)
  , array('name' ' => 'jack', 'age' => 22)
);

I hope to sort by age from small to large. The author sorted out two methods and shared them with you.
1. Using this method with array_multisort
will be more troublesome. It is necessary to extract the age and store it in a one-dimensional array, and then arrange it in ascending order of age. The specific code is as follows: The
code is as follows:

$ages = array();
foreach ($users as $user) {
  $ages[] = $user['


After execution, $users is a sorted array, you can print it out to see. If you need to sort by age in ascending order, and then sort by name in ascending

order method is called like this: The
code is as follows:

array_multisort($ages, SORT_ASC, $names, SORT_ASC, $users );

The point here is to first store the key to be sorted in a one-dimensional array, and then you can use the function array_multisort() to sort the array according to the key. Of course, you can sort here

without array_multisort () This function
can achieve this effect only through foreach traversal, but since the PHP developers have provided us with a better way, we can save unnecessary trouble.

PHP two-dimensional array deduplication function
PHP array deduplication has a built-in function array_unique (), but PHP's array_unique function is only suitable for one-dimensional arrays, not for multi-dimensional arrays, the following provides a two-dimensional array 

array_unique function
    function unique_arr($array2D,$stkeep=false,$ndformat=true)  
    {  
        // Determine whether to retain the first-level array keys (the first-level array keys can be non-numeric)  
        if($stkeep) $stArr = array_keys($array2D);  
      
        / / Determine whether to keep the secondary array keys (all secondary array keys must be the same)  
        if($ndformat) $ndArr = array_keys(end($array2D));  
      
        //Dimensionality reduction, you can also use implode to convert a one-dimensional array to a string connected with commas  
        foreach ($array2D as $v){  
            $v = join(",",$v);   
            $temp[] = $v;  
        }  
      
        //Remove duplicate strings, that is, duplicate one-dimensional arrays  
        $temp = array_unique($temp);   
      
        //And then split Array reassembly  
        foreach ($temp as $k => $v)  
        {  
            if($stkeep) $k = $stArr[$k];  
            if($ndformat)  
            {  
                $tempArr = explode(",",$v) ;   
                foreach($tempArr as $ndkey => $ndval) $output[$k][$ndArr[$ndkey]] = $ndval;  
            }  
            else $output[$k] = explode(",",$v);   
        }  
      
        return $output;  
    }  

$array2D=array('first'=>array('title'=>'1111','date'=>'2222'),'second'=>array('title'=>'1111','date'=>'2222'),'third'=>array

('title'=>'2222','date'=>'3333'));  
    print_r($array2D);  
    print_r(unique_arr($array2D,true));  









Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324748505&siteId=291194637
Recommended