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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Chinese version
Chinese version, very easy to use
