How to assign value to array in php

silencement
Release: 2023-02-25 06:50:02
Original
8285 people have browsed it

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
//设置某个变量为一个空数组
$arr=array();
?>
Copy after login

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]=&#39;苹果&#39;;
Copy after login

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(&#39;0&#39;=>&#39;苹果&#39;);
Copy after login

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(&#39;苹果&#39;);
Copy after login

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');

<?php
//请创建一个数组变量arr,并尝试创建一个关联数组,键是apple,值是苹果
$arr = array(&#39;apple&#39;=>&#39;苹果&#39;);
if( isset($arr) ) {print_r($arr);}
?>
//Array ( [apple] => 苹果 )
Copy after login
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!

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