Home > Backend Development > PHP Tutorial > Some arrays in php

Some arrays in php

不言
Release: 2023-03-24 18:02:01
Original
1069 people have browsed it

This article mainly introduces some arrays about php, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

Simulating data structure

After array Add

array_push
$a=array("red","green");
array_push($a,"blue","yellow");
Array ( [0] => red [1] => green [2] => blue [3] => yellow )
Copy after login

Add before array

array_unshift
$a=array("a"=>"red","b"=>"green");
array_unshift($a,"blue");
Array ( [0] => blue [a] => red [b] => green )
Copy after login

Delete before array

$a=array("a"=>"red","b"=>"green","c"=>"blue");
echo array_shift($a);
redArray ( [b] => green [c] => blue )
Copy after login

Delete after array

$a=array("red","green","blue");
array_pop($a);
Array ( [0] => red [1] => green )
Copy after login

Basics of PHP - Two-dimensional array sorting array_multisort

Sorting two-dimensional arrays or multi-dimensional arrays is a common problem. In PHP, we have a special multi-dimensional array sorting function. Here is a brief introduction:
array_multisort (array1,sorting order, sorting type,array2,array3..) is a function that sorts multiple arrays or multidimensional arrays.
array1 Required. Specifies the input array.
sorting order Optional. Specify the order of sorting. Possible values ​​are SORT_ASC and SORT_DESC.
sorting type Optional. Specifies the sorting type. Possible values ​​are SORT_REGULAR, SORT_NUMERIC, and SORT_STRING.
array2 Optional. Specifies the input array.
array3 Optional. Specifies the input array.
The array in the parameter is treated as a table column and sorted by row - this is similar to the function of SQL's ORDER BY clause. The first array is the main array to be sorted. If the rows (values) in the array are compared to be the same,

will be sorted according to the size of the corresponding value in the next input array, and so on.
The first parameter is an array, and each subsequent parameter may be an array or one of the following sort order flags (the sort flag is used to change the default sort order):
SORT_ASC - Default, in ascending order arrangement. (A-Z)
SORT_DESC - Sort in descending order. (Z-A)
You can then specify the sorting type:
SORT_REGULAR - Default. Arrange each item in regular order.
SORT_NUMERIC - Arrange each item in numerical order.
SORT_STRING - Arrange each item in alphabetical order

    <?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(&#39;id&#39;=>1,&#39;name&#39;=>&#39;fj&#39;,&#39;weight&#39;=>100,&#39;height&#39;=>180),  
                        array(&#39;id&#39;=>2,&#39;name&#39;=>&#39;tom&#39;,&#39;weight&#39;=>53,&#39;height&#39;=>150),  
                        array(&#39;id&#39;=>3,&#39;name&#39;=>&#39;jerry&#39;,&#39;weight&#39;=>120,&#39;height&#39;=>156),  
                        array(&#39;id&#39;=>4,&#39;name&#39;=>&#39;bill&#39;,&#39;weight&#39;=>110,&#39;height&#39;=>190),  
                        array(&#39;id&#39;=>5,&#39;name&#39;=>&#39;linken&#39;,&#39;weight&#39;=>80,&#39;height&#39;=>200),  
                        array(&#39;id&#39;=>6,&#39;name&#39;=>&#39;madana&#39;,&#39;weight&#39;=>95,&#39;height&#39;=>110),  
                        array(&#39;id&#39;=>7,&#39;name&#39;=>&#39;jordan&#39;,&#39;weight&#39;=>70,&#39;height&#39;=>170)  
                    );  
        var_dump($person);  
        $person = my_sort($person,&#39;name&#39;,SORT_ASC,SORT_STRING);  
        var_dump($person);   
        $person = my_sort($person,&#39;weight&#39;);  
        var_dump($person);  
    ?>
Copy after login

For example, the following array:
The code is as follows:

$users = array(
  array(&#39;name&#39; => &#39;tom&#39;, &#39;age&#39; => 20)
  , array(&#39;name&#39; => &#39;anny&#39;, &#39;age&#39; => 18)
  , array(&#39;name&#39; => &#39;jack&#39;, &#39;age&#39; => 22)
);
Copy after login

Hope it can Sort by age from small to large. The author has sorted out two methods and shared them with you.
1. Use array_multisort
Using this method will be more troublesome. You need to extract the age and store it in a one-dimensional array, and then arrange it in ascending order by age. The specific code is as follows:
The code is as follows:

$ages = array();
foreach ($users as $user) {
  $ages[] = $user[&#39;age&#39;];
}
Copy after login
array_multisort($ages, SORT_ASC, $users);
Copy after login


After execution, $users will be a sorted array, which can be printed out to see. If you need to sort by age in ascending order first, and then by name in ascending order, the method is the same as above, that is, extract an additional name array, and the final sorting method

is called like this:
The code is as follows:

array_multisort($ages, SORT_ASC, $names, SORT_ASC, $users);
Copy after login



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

not apply the array_multisort() function for sorting here.
This effect can also be achieved only through foreach traversal, but since PHP developers have provided us with a better way, We can save ourselves unnecessary trouble.

PHP two-dimensional array deduplication function
PHP array has a built-in function array_unique () to remove duplicates, but PHP's array_unique function only applies to one-dimensional arrays, not multi-dimensional arrays, as follows

array_unique function that provides a two-dimensional array

    function unique_arr($array2D,$stkeep=false,$ndformat=true)  
    {  
        // 判断是否保留一级数组键 (一级数组键可以为非数字)  
        if($stkeep) $stArr = array_keys($array2D);  
      
        // 判断是否保留二级数组键 (所有二级数组键必须相同)  
        if($ndformat) $ndArr = array_keys(end($array2D));  
      
        //降维,也可以用implode,将一维数组转换为用逗号连接的字符串  
        foreach ($array2D as $v){  
            $v = join(",",$v);   
            $temp[] = $v;  
        }  
      
        //去掉重复的字符串,也就是重复的一维数组  
        $temp = array_unique($temp);   
      
        //再将拆开的数组重新组装  
        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(&#39;first&#39;=>array(&#39;title&#39;=>&#39;1111&#39;,&#39;date&#39;=>&#39;2222&#39;),&#39;second&#39;=>array(&#39;title&#39;=>&#39;1111&#39;,&#39;date&#39;=>&#39;2222&#39;),&#39;third&#39;=>array
(&#39;title&#39;=>&#39;2222&#39;,&#39;date&#39;=>&#39;3333&#39;));  
    print_r($array2D);  
    print_r(unique_arr($array2D,true));
Copy after login

Related recommendations:

php array function sequence array_pop() deletes the last one in the array Element

How to assign value to PHP array

The above is the detailed content of Some arrays in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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