Home  >  Article  >  Backend Development  >  How to pass values ​​to array in php

How to pass values ​​to array in php

PHPz
PHPzOriginal
2023-04-18 10:19:02470browse

In PHP, array is a very important data structure that can store multiple elements. These elements can be any type of data, such as numbers, strings, objects, etc. When using an array, the most common operation is to add elements to the array, that is, to pass values ​​to the array. Below we will introduce some methods of passing values ​​to arrays in PHP.

  1. Declare the array directly

The first method is to declare the array directly in PHP and assign a value to the array. This is done by assigning the value of each element to each key of the array using square brackets and comma delimiters. For example, the following code creates an array with four elements:

$arr = ['apple', 'banana', 'orange', 'grape'];

In this example, we create an $arr array that contains four string elements: 'apple', 'banana ', 'orange' and 'grape'.

  1. Use the array() function

The second method is to use the array() function provided by PHP. This function needs to pass in one or more parameters, each parameter represents an element of the array. For example, the following code creates an array containing four elements:

$arr = array('apple', 'banana', 'orange', 'grape');

In this example, we use the array() function to create the $arr array, which also contains four string elements.

  1. Assignment by key name

Sometimes, we want to specify the key name of each element when creating an array. At this time, we can use the following syntax:

$arr = array(
    'name' => 'Tom',
    'age' => 25,
    'gender' => 'male'
);

In this example, we create an associative array $arr, which contains three key-value pairs. The key names are 'name', 'age' and 'gender', and the corresponding values ​​are the string 'Tom', the integer 25 and the string 'male'.

  1. Using the [] operator

Starting from PHP 5.4 version, we can use the square bracket [] operator to add elements to the array. The specific method is to declare an empty array according to the following syntax, and then add elements one by one:

$arr = [];
$arr[] = 'apple';
$arr[] = 'banana';
$arr[] = 'orange';
$arr[] = 'grape';

In this example, we first create an empty array $arr, and then assign values ​​one by one. Note that this method only applies to sequential arrays, not associative arrays.

Summary

In PHP, there are many ways to pass values ​​into an array. We can declare an array directly, using the array() function, by key assignment, or using the [] operator. In actual development, we need to choose the most appropriate method according to specific scenarios to improve the efficiency and readability of the code. I hope this article can help PHP beginners better master the use of arrays.

The above is the detailed content of How to pass values ​​to array in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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