PHP Array usage

Keywords: PHP Linux Javascript

Catalog

What is array

Array is one of the data structures, which is widely used in Web scenarios. Knowing how to use array can improve reading efficiency.
Most references are as follows: https://segmentfault.com/a/1190000002723270#articleHeader9


array operator

To access the contents of a variable, you can use its name directly.

$a=996;

If the variable is an array, you can use a combination of the variable name and keyword or index to access its contents.

$name_array=array("lisi","zhangsan");

Like other variables, you can change the contents of an array element by using the operator =. Array units can be accessed through the array[key] syntax.

Array operator:

The array operator, which is rarely used, is used in web scenarios.


basic operation

CURD

Create array

There are two ways to declare arrays in PHP:

1. Use the array() function to declare an array:

# 1
$array = array();  
$array["key"] = "values";   

#2
$users = array('phone','computer','dos','linux');
echo $users;//Only the data type Array will be printed out
print_r($users);//Array ( [0] => phone [1] => computer [2] => dos [3] => linux )

2. Assign values to array elements directly:

$numbers = range(1,5);//Create an array with the specified range
print_r($numbers);//Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )

Traversing array

//Display all the values in the array through a loop
    for($i = 0 ;$i < 5;$i++){
        echo $users[$i];
        echo '<br/>';
    }

//count/sizeof count the number of cells in an array or the number of properties in an object
    for($i = 0; $i < count($users);$i++){
        echo $users[$i];
        echo '<br/>';
    }

//You can also traverse the array through the foreach loop, which has the advantage that you do not need to consider the key
    foreach($users as $value){
        echo $value.'<br/>';//The point number is a string connection symbol
    }

//foreach loop traverses $key = > $value; $key and $value are variable names, which can be set by yourself
    foreach($users as $key => $value){
        echo $key.'<br/>';//Output key
    }

Create an array of type K=V

From php5.4 onwards, you can use the short array definition syntax to replace array() with []. It is similar to the definition of array in javascript;

//Create an array of custom keys
    $ceo = array('apple'=>'jobs','microsoft'=>'Nadella','Larry Page','Eric');

//If you don't declare the key of an element, it starts from zero
    print_r($ceo);//Array ( [apple] => jobs [microsoft] => Nadella [0] => Larry Page [1] => Eric )

    echo $ceo['apple']; //jobs

//Usage of php5.4
    $array = [
        "foo" => "bar",
        "bar" => "foo",
    ];

    print_r($array); //Array ( [foo] => bar [bar] => foo ) 

Delete

change

check


each( )

The pointer of each points to the first key value pair, returns the first array element, obtains its key value pair, and wraps it into a new array

$users = array('trigkit4'=>22,'mike'=>20,'john'=>30);
//print_r(each($users));//Array ( [1] => 22 [value] => 22 [0] => trigkit4 [key] => trigkit4 )

//Equivalent to: $a = array ([0] = > trigkit4, [1] = > 22, [value] = > 22, [key] = > trigkit4);
$a = each($users);//each takes out the first element of the original array, wraps it into a new array, and assigns it to $a
echo $a[0];//trigkit4
echo "<br/>";
echo $a[1];//22


echo "<br/>";
var_dump($a);

Output:

trigkit4
22
array(4) { [1]=> int(22) ["value"]=> int(22) [0]=> string(8) "trigkit4" ["key"]=> string(8) "trigkit4" }


list( )

Posted by k994519 on Sat, 16 Nov 2019 07:54:31 -0800