Home  >  Article  >  Backend Development  >  How to remove key values ​​from a two-dimensional array in php

How to remove key values ​​from a two-dimensional array in php

PHPz
PHPzOriginal
2023-04-27 09:00:581274browse

1. Preface

In programming languages, arrays are a common data structure that can easily store and manipulate large amounts of data. PHP, as a popular dynamic scripting language, also supports arrays. In PHP, an array can be defined as a variable where each element has a unique key and a value. PHP arrays are usually two-dimensional arrays, that is, an array contains one or more arrays. This article will explore how to retrieve the key values ​​​​in a two-dimensional array in PHP.

2. What is a two-dimensional array?

In PHP, a two-dimensional array means that the elements of an array are also an array. In other words, we can say that each element is an array consisting of key-value pairs. For example, a two-dimensional array named $employees can contain multiple arrays named $employee1, $employee2, and $employee3, each containing keys and values. You can use the following syntax to define a two-dimensional array:

$employees = [
    ['name' => 'John', 'age' => 28, 'salary' => 5000],
    ['name' => 'Tom', 'age' => 32, 'salary' => 6000],
    ['name' => 'Mary', 'age' => 25, 'salary' => 4500]
];

In the above code, you can see a $employees two-dimensional array containing three arrays. Each array has three keys: 'name', 'age' and 'salary'. In this way, a two-dimensional array can easily organize large amounts of data.

3. How to remove the key values ​​from the two-dimensional array?

To access key values ​​in a two-dimensional array, you can use the following four methods.

  1. Using a for loop

You can use a for loop to traverse each element in a two-dimensional array and obtain the key and value of each element. As shown below:

$employees = [
    ['name' => 'John', 'age' => 28, 'salary' => 5000],
    ['name' => 'Tom', 'age' => 32, 'salary' => 6000],
    ['name' => 'Mary', 'age' => 25, 'salary' => 4500]
];

for ($i = 0; $i < count($employees); $i++) {
    foreach ($employees[$i] as $key => $value) {
        echo "$key: $value
";     } }

The above code will iterate through each element in the $employees array, and then use a foreach loop to obtain the key and value of each element. Because the entire array is traversed, each key and value can be printed out. Run the above code, the output is as follows:

name: John
age: 28
salary: 5000
name: Tom
age: 32
salary: 6000
name: Mary
age: 25
salary: 4500
  1. Use foreach loop

You can use foreach loop to traverse each element in the two-dimensional array and obtain the value of each element keys and values. As shown below:

$employees = [
    ['name' => 'John', 'age' => 28, 'salary' => 5000],
    ['name' => 'Tom', 'age' => 32, 'salary' => 6000],
    ['name' => 'Mary', 'age' => 25, 'salary' => 4500]
];

foreach ($employees as $employee) {
    foreach ($employee as $key => $value) {
        echo "$key: $value
";     } }

In the above code, a foreach loop is used to iterate through each element in the $employees array and get the key and value respectively. Since the array is two-dimensional, nested loops are required. Running the above code, the output is as follows:

name: John
age: 28
salary: 5000
name: Tom
age: 32
salary: 6000
name: Mary
age: 25
salary: 4500

Compared with using a for loop, using a foreach loop is more concise and efficient.

  1. Using the array_column function

PHP provides an array_column function that can obtain a single column from a two-dimensional array and use the column as a new one Dimensional array returned. As shown below:

$employees = [
    ['name' => 'John', 'age' => 28, 'salary' => 5000],
    ['name' => 'Tom', 'age' => 32, 'salary' => 6000],
    ['name' => 'Mary', 'age' => 25, 'salary' => 4500]
];

$salaries = array_column($employees, 'salary');

print_r($salaries);

In the above code, use the array_column function to get the 'salary' value in the $employees array. After calling this function, an array is returned containing all the 'salary' key values ​​in the $salaries array.

Run the above code, the output is as follows:

Array ( [0] => 5000 [1] => 6000 [2] => 4500 )

As you can see, a one-dimensional array containing three elements is returned.

  1. Using the key name

You can use the key name to get the value of the element in the two-dimensional array. As shown below:

$employees = [
    ['name' => 'John', 'age' => 28, 'salary' => 5000],
    ['name' => 'Tom', 'age' => 32, 'salary' => 6000],
    ['name' => 'Mary', 'age' => 25, 'salary' => 4500]
];

$salary_one = $employees[0]['salary'];
$salary_two = $employees[1]['salary'];
$salary_three = $employees[2]['salary'];

echo "Salary 1: $salary_one
"; echo "Salary 2: $salary_two
"; echo "Salary 3: $salary_three
";

In the above code, get the 'salary' value of each element by key name and print it out.

Run the above code, the output is as follows:

Salary 1: 5000
Salary 2: 6000
Salary 3: 4500

IV. Summary

This article introduces how to remove the key values ​​​​in a two-dimensional array in PHP. Specifically, you can use for loops, foreach loops, array_column functions or key names to access and operate two-dimensional arrays. These methods are very simple and easy to follow and useful for both beginners and experienced developers. I hope readers can learn relevant knowledge about arrays in PHP from this article and apply them in their daily work.

Reference:

  • PHP - Multidimensional Arrays. Retrieved from https://www.tutorialspoint.com/php/php-multidimensional-arrays.htm.
  • PHP - Arrays. Retrieved from https://www.w3schools.com/php/php_arrays.asp.

The above is the detailed content of How to remove key values ​​from a two-dimensional 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