search
HomeBackend DevelopmentPHP ProblemHow to remove the key of a two-dimensional array in PHP

In PHP development, we often use arrays to store some data, and each element in the array consists of a key and a value. In some cases, we need to remove the keys from the array and retain only the values ​​in the array. This requires the use of some functions provided in PHP to remove array keys.

So, how to remove the key of a two-dimensional array? Here are some commonly used methods.

1. Use the array_values() function

The array_values() function will return an array containing only element values, not the keys and values ​​in the original array. This function converts a two-dimensional array into a one-dimensional array, retaining all values.

For example:

$array = array(
    array('id'=>1,'name'=>'张三'),
    array('id'=>2,'name'=>'李四')
);
$array = array_map('array_values',$array);
print_r($array);

The output result is:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 张三
        )

    [1] => Array
        (
            [0] => 2
            [1] => 李四
        )
)

2. Use foreach loop

Another way to remove the key of a two-dimensional array is to use foreach Loop through each element and store the value of each element into a new array. During this process, we can use the unset() function to delete the key and keep only the value.

For example:

$array = array(
    array('id'=>1,'name'=>'张三'),
    array('id'=>2,'name'=>'李四')
);
$newArray = array();
foreach($array as $item){
    $newArray[] = array_values($item);
}
print_r($newArray);

The output result is:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 张三
        )

    [1] => Array
        (
            [0] => 2
            [1] => 李四
        )
)

3. Use the array_map() function

array_map() function to convert the incoming array into Each element is passed to the callback function for processing, and a new array is returned. The elements of the array are the results of applying the callback function to each element. We can use the array_values() function in the callback function to remove the keys of the array.

For example:

$array = array(
    array('id'=>1,'name'=>'张三'),
    array('id'=>2,'name'=>'李四')
);
$newArray = array_map(function($item){
    return array_values($item);
}, $array);
print_r($newArray);

The output result is:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 张三
        )

    [1] => Array
        (
            [0] => 2
            [1] => 李四
        )
)

Summary

The above are three methods to remove keys from two-dimensional arrays in PHP. In actual development, we can choose the most suitable method according to the specific situation. Whichever method you use, you should take care to preserve the values ​​of the array and keep your code as concise and readable as possible.

The above is the detailed content of How to remove the key of 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

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

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!