Home  >  Article  >  Backend Development  >  How to modify age in php array

How to modify age in php array

PHPz
PHPzOriginal
2023-04-25 10:32:41447browse

1. Preface

Array is a very commonly used data structure in writing PHP code. It can store multiple values ​​and enable fast traversal and manipulation. In actual projects, we often need to modify arrays. This article will take modifying the age in an array as an example to introduce the method of modifying an array in PHP.

2. Basic concepts of arrays

In PHP, an array is a composite type of data structure, which consists of a set of key-value pairs. Arrays can be accessed by index or by association. The following is how an example array is defined:

// 索引数组
$car_brands = array("Benz", "BMW", "Audi", "Lexus");

// 关联数组
$car_prices = array("Benz"=>"500000", "BMW"=>"600000", "Audi"=>"400000", "Lexus"=>"700000");

Among them, the index array refers to an array whose subscripts are numbers, also called a sequential array. An associative array is an array whose subscript is a string, also known as a hash array. In an array, subscripts can be repeated, but values ​​cannot be repeated.

3. Modify the values ​​in the array

There are two situations when using PHP to modify the values ​​in the array, namely modifying the values ​​in the index array and modifying the values ​​in the associative array.

  1. Modify the value in the index array

To modify the value in the index array, you need to know the position of the element to be modified. Suppose we have an array that stores personnel information, and the elements in it are associative arrays with age as the array subscript. Then we can use the following method to modify the age of a person in the array:

//定义关联数组,以年龄作为数组下标
$person_info = array(
    "18" => "小明",
    "22" => "小红",
    "30" => "小黄"
);

//修改小明的年龄为19岁
$person_info["19"] = $person_info["18"];
unset($person_info["18"]);

print_r($person_info);

In the above code, we First, change Xiao Ming's age from 18 to 19, and then delete the original 18 subscripted element through the unset function. Output the modified array elements through the print_r function, and the result is as follows:

Array
(
    [19] => 小明
    [22] => 小红
    [30] => 小黄
)
  1. Modify the value in the associative array

In the associative array, the elements are strings. subscripted. Find the corresponding value through the subscript and you can modify it. Suppose we have an array that stores personnel information, an associative array with the name as the subscript of the array, then the age of a certain person can be modified in the following way:

//定义关联数组,以姓名作为数组下标
$person_info = array(
    "小明" => "18",
    "小红" => "22",
    "小黄" => "30"
);

//修改小明的年龄为19岁
$person_info["小明"] = "19";

print_r($person_info);

In the above code, we directly modify the subscript to " "Xiao Ming" element value, thereby modifying the age of the corresponding person. Output the modified array through the print_r function, and the result is as follows:

Array
(
    [小明] => 19
    [小红] => 22
    [小黄] => 30
)

4. Modify the values ​​of multiple elements in the array

If you want to modify the values ​​of multiple elements in the array, you need to This is accomplished by looping through the array. The following is a sample code, which is an example of calculating BMI and judging health status based on height and weight. We can modify multiple elements by modifying the height and weight of different people in the array:

//定义关联数组,以姓名作为数组下标
$person_info = array(
    "小明" => array("height"=>175,"weight"=>75),
    "小红" => array("height"=>165,"weight"=>55),
    "小黄" => array("height"=>180,"weight"=>65)
);

//循环遍历数组,计算BMI并输出
foreach ($person_info as $name => $value) {
    $bmi = $value["weight"] / (($value["height"]/100) * ($value["height"]/100));
    echo $name."的BMI为:".$bmi.",“健康状况”为:";
    if ($bmi < 18.5) {
        echo "体重过轻\n";
    } elseif ($bmi >= 18.5 && $bmi < 24) {
        echo "健康体重\n";
    } elseif ($bmi >= 24 && $bmi < 28) {
        echo "超重\n";
    } else {
        echo "肥胖\n";
    }
}

//修改小明的身高和体重
$person_info["小明"]["height"] = 180;
$person_info["小明"]["weight"] = 80;

echo "\n修改后的小明的数据为:\n";
print_r($person_info["小明"]);

Above In the code, a multi-dimensional array is first defined to save the height and weight of different people. By looping through the array, the BMI value of the corresponding person is calculated and output. After that, we modified Xiao Ming’s height and weight, and then output the modified height and weight through the print_r function. The results obtained are as follows:

小明的BMI为:24.489795918367,"健康状况"为:超重
小红的BMI为:20.20202020202,"健康状况"为:健康体重
小黄的BMI为:20.061728395062,"健康状况"为:健康体重

修改后的小明的数据为:
Array
(
    [height] => 180
    [weight] => 80
)

5. Summary

Arrays are commonly used in PHP One of the data structures, the method of operating arrays is also relatively common. Modifying elements in an array is one of the basic operations in array operations. It requires us to have an in-depth understanding of the array structure and master the corresponding syntax methods before we can use it more freely in actual development. This article introduces how to use arrays to modify age in PHP, hoping to provide a reference for readers.

The above is the detailed content of How to modify age in php array. 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