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.
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>'; } ?>
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.
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); ?>
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.
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); ?>
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!