Remove key values from php two-dimensional array
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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8's JIT compilation enhances performance by compiling frequently executed code into machine code, benefiting applications with heavy computations and reducing execution times.

The article discusses implementing robust authentication and authorization in PHP to prevent unauthorized access, detailing best practices and recommending security-enhancing tools.

The article discusses symmetric and asymmetric encryption in PHP, comparing their suitability, performance, and security differences. Symmetric encryption is faster and suited for bulk data, while asymmetric is used for secure key exchange.

Article discusses retrieving data from databases using PHP, covering steps, security measures, optimization techniques, and common errors with solutions.Character count: 159

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses the mysqli_query() and mysqli_fetch_assoc() functions in PHP for MySQL database interactions. It explains their roles, differences, and provides a practical example of their use. The main argument focuses on the benefits of usin

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

The article discusses strategies to prevent CSRF attacks in PHP, including using CSRF tokens, Same-Site cookies, and proper session management.
