Home > Backend Development > PHP Problem > Remove key values ​​from php two-dimensional array

Remove key values ​​from php two-dimensional array

王林
Release: 2023-05-06 12:30:08
Original
515 people have browsed it

In PHP, array is a very important data structure. In actual development, two-dimensional arrays are often used. Sometimes, we need to remove the key values ​​​​in the two-dimensional array and only take the values ​​​​in the array. This article will introduce how to implement the method of removing key values ​​from a two-dimensional array in PHP.

  1. foreach loop traversal

The simplest way to remove the key values ​​​​in the php two-dimensional array is to use a foreach loop to traverse the array. The following is an implemented code example:

<?php
$students = array(
    array('name' => 'Tom', 'age' => 18),
    array('name' => 'Jerry', 'age' => 20),
    array('name' => 'Mike', 'age' => 22)
);

foreach ($students as $student) {
    echo $student['name'] . ' ' . $student['age'] . '<br>';
}
?>
Copy after login

A two-dimensional array $students is defined here, which contains three subarrays: Tom, Jerry and Mike. Then it traverses the $students array through a foreach loop and outputs the name and age values ​​of each sub-array to the page.

  1. array_map() function processing

Another way to remove key values ​​from a PHP two-dimensional array is to use the array_map() function. What this function does is pass all array values ​​as parameters to the callback function and then return a new array. The following sample code shows how to use the array_map() function to process a two-dimensional array:

<?php
$students = array(
    array('name' => 'Tom', 'age' => 18),
    array('name' => 'Jerry', 'age' => 20),
    array('name' => 'Mike', 'age' => 22)
);

$result = array_map(function($student) {
    return array_values($student);
}, $students);

print_r($result);
?>
Copy after login

A two-dimensional array $students is defined here, and then the key value of each sub-array is removed through the array_map() function. The specific implementation is to use an anonymous function to pass the subarray to the array_values() function, which will return a new array containing only values. Finally, print the results to the page.

  1. Use the array_column() function

In PHP7.0 version, a new function array_column() is introduced, which can simplify the code to implement PHP two-dimensional array without key values. . The following is a code example for implementing this method:

<?php
$students = array(
    array('name' => 'Tom', 'age' => 18),
    array('name' => 'Jerry', 'age' => 20),
    array('name' => 'Mike', 'age' => 22)
);

$result = array_column($students, null);

print_r($result);
?>
Copy after login

A two-dimensional array $students is also defined here. Then use the array_column() function to extract the value of each subarray in the $students array and return a new array. The null parameter means to extract all values ​​and remove the key names. Finally, print the results to the page.

Summary

The above are three ways to remove key values ​​from a two-dimensional array in PHP. Whether it is foreach loop traversal, using the array_map() function or using the array_column() function, it is easy to implement. Different methods can be selected according to specific application scenarios.

The above is the detailed content of Remove key values ​​from php two-dimensional array. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template