Home  >  Article  >  Backend Development  >  How to replace k value in 2D array in PHP

How to replace k value in 2D array in PHP

PHPz
PHPzOriginal
2023-04-19 10:06:53579browse

PHP is a widely used programming language. It is easy to learn, flexible, efficient and open source. Therefore, it has become the language of choice for many enterprises and developers. In PHP, two-dimensional arrays are also one of the most common data structures. They can store large amounts of data and perform fast data query and traversal. This article will focus on how to replace the k value in a two-dimensional array in PHP.

Two-dimensional array

In PHP, array is a very important data type. Arrays can store multiple values ​​under one variable name, and these values ​​can be of different data types, such as strings, integers, objects, etc.

Arrays in PHP are divided into one-dimensional arrays and multi-dimensional arrays. Among them, multi-dimensional arrays are divided into two-dimensional arrays, three-dimensional arrays, four-dimensional arrays, etc. A two-dimensional array refers to an array containing multiple one-dimensional arrays.

The sample code to define a two-dimensional array is as follows:

$students = array(
   array("name"=>"张三", "age"=>18, "score"=>85),
   array("name"=>"李四", "age"=>19, "score"=>90),
   array("name"=>"王五", "age"=>20, "score"=>75)
);

The above code defines a two-dimensional array named $students, which contains three one-dimensional arrays. Each one-dimensional array contains three elements, representing the student's name, age, and grade.

Replace the k value in the two-dimensional array

Sometimes, we need to replace some k values ​​in the two-dimensional array. To achieve this goal, we can use the array_map function and array_column function provided in PHP. The specific steps are as follows:

The first step is to use the array_column function to obtain a column of the original array and generate a new one-dimensional array. The k value corresponding to this column is the k value we want to replace.

The sample code is as follows:

$key_array = array_column($students, 'name');

This code extracts the name key value of each one-dimensional array in the $students array to form a new one-dimensional array $key_array.

The second step is to use the array_map function to traverse the new one-dimensional array and replace the value of each element through the callback function. In the callback function, use the value of each element as the new k value, find the one-dimensional array corresponding to this k value in the original two-dimensional array, and replace its k value with the new k value.

The sample code is as follows:

$new_key_array = array_map(function($val) use ($students) {
      foreach($students as &$student) {
         if($student['name'] == $val) {
            $student['new_name'] = $student['name'];
            unset($student['name']);
            return $student;
         }
      }  
}, $key_array);

This code traverses the $key_array array. For each element $val, it traverses the original array $students through a foreach loop and finds $val in the original array. The corresponding one-dimensional array and replaces its name key value with a new key value new_name. Finally, delete the original name key in the one-dimensional array and return the modified one-dimensional array.

The third step is to check the new array to confirm whether the replacement operation takes effect.

The final code is as follows:

$students = array(
  array("name"=>"张三", "age"=>18, "score"=>85),
  array("name"=>"李四", "age"=>19, "score"=>90),
  array("name"=>"王五", "age"=>20, "score"=>75)
);

$key_array = array_column($students, 'name');

$new_key_array = array_map(function($val) use ($students) {
  foreach($students as &$student) {
     if($student['name'] == $val) {
        $student['new_name'] = $student['name'];
        unset($student['name']);
        return $student;
     }
  }  
}, $key_array);

print_r($students);

Running the above code, we can get the following output:

Array
(
    [0] => Array
        (
            [age] => 18
            [score] => 85
            [new_name] => 张三
        )

    [1] => Array
        (
            [age] => 19
            [score] => 90
            [new_name] => 李四
        )

    [2] => Array
        (
            [age] => 20
            [score] => 75
            [new_name] => 王五
        )
)

As can be seen from the output, we have successfully converted the original The name key is replaced with a new key new_name.

Summary

In PHP, replacing the k value in a two-dimensional array is a relatively common operation. By using the array_map function and array_column function, we can easily implement this operation. In actual development, we need to choose the most appropriate method to implement this operation according to the specific situation to improve development efficiency and code quality.

The above is the detailed content of How to replace k value in 2D 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