"Tom","/> "Tom",">

Home >Backend Development >PHP Problem >How to assign values ​​to multidimensional arrays in php

How to assign values ​​to multidimensional arrays in php

WBOY
WBOYOriginal
2023-05-19 09:31:07693browse

PHP is a very popular server-side scripting language that supports the creation of multi-dimensional arrays. A multidimensional array is an array that contains subarrays within an array. This gives us more flexibility in managing our data because it can be grouped together as needed.

When we operate multi-dimensional arrays, we may need to assign new data. This article will introduce how to assign multidimensional arrays in PHP.

First, suppose we already have a multi-dimensional array, for example:

$students = array(
    array("name" => "Tom", "age" => 20, "gender" => "male"),
    array("name" => "Mary", "age" => 18, "gender" => "female"),
    array("name" => "John", "age" => 22, "gender" => "male")
);

This is an array containing three sub-arrays, each sub-array contains the name, age and gender of the student. Now, we want to add scores for each student.

The first method is to use a loop to go through the array and add the score for each student, for example:

foreach ($students as &$student) {
    $student["score"] = 90;
}

The & notation is used here to refer to each sub-array, so that they can be modified within the loop. We use the [] operator to add a key "score" and a value of 90 for each student. Now, the $students array becomes like this:

Array (
    [0] => Array ( [name] => Tom [age] => 20 [gender] => male [score] => 90 )
    [1] => Array ( [name] => Mary [age] => 18 [gender] => female [score] => 90 )
    [2] => Array ( [name] => John [age] => 22 [gender] => male [score] => 90 )
)

The second method is to add new keys and values ​​based on the original multi-dimensional array, for example:

$students[0]["score"] = 90;
$students[1]["score"] = 90;
$students[2]["score"] = 90;

Here, use array index access directly each subarray and add a key "score" and a value of 90 to them. This method is simpler than the first method, but if the array is very large, it may be more efficient to loop through it.

In addition to adding keys and values, we can also update the values ​​of multi-dimensional arrays in a similar way. For example:

$students[0]["age"] = 21;

The age of the first student is updated here to be 21. The subarray that needs to be updated can be accessed through the array index and key name.

Assigning multidimensional arrays in PHP is very simple, just use the array index and key name. We can add or update key-value pairs using a loop or by directly accessing a single sub-array. This enables a more flexible way to manage and operate data, making development more convenient and efficient.

The above is the detailed content of How to assign values ​​to multidimensional arrays 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