Home>Article>Backend Development> How to assign value to array in php
An array is a language structure composed of key-value pairs. The key is similar to the hotel room number, and the value is similar to the things stored in the hotel room.
Recommended manual: php complete self-study manual
If you go to a hotel to stay, the waiter will tell you the room number is How much is stored in the room? You need to enter the room according to the room number to know.
PHP has two types of arrays:
Index array and associative array. The two words index and association refer to the keys of the array.
Index array assignment of PHP array
There are three ways to assign index array:
The first one: Use the name of the array variable followed by a Chinese character Values are assigned in brackets. Of course, in the index array, the keys in the brackets must be integers.
比如,$arr[0]='苹果';
Second: Use array() to create an empty array, use the => symbol to separate keys and values, the left side represents the key, and the right side represents the value. Of course, in the index array, the keys must be integers. For example,
array('0'=>'苹果');
The third method: Use array() to create an empty array, and directly assign values in the array using English single quotes ' or English double quotes ". The array will be created starting from 0 by default. The key of the integer.
比如array('苹果');
This array is equivalent to array('0'=>'Apple');
Associative array assignment of PHP array
There are two ways to assign values to associative arrays:
The first one: use the name of the array variable followed by a square bracket to assign the value. Of course, in an associative array, the key within the square brackets must be Is a string.
For example, $arr['apple']='Apple';
Second: Use array() to create an empty array and use => symbols to separate it Keys and values, the left side represents the key, and the right side represents the value. Of course, in an associative array, the key must be a string.
For example, array('apple'=>'Apple');
'苹果'); if( isset($arr) ) {print_r($arr);} ?> //Array ( [apple] => 苹果 )
Recommended related articles:
1. How to define an array in PHP? How many ways to define an array?
2. php What are the commonly used array functions? Summary of commonly used array functions in PHP
Related video recommendations:
1. Dugu Jiujian(4)_PHP video tutorial
The above is the detailed content of How to assign value to array in php. For more information, please follow other related articles on the PHP Chinese website!