Home >Backend Development >PHP Problem >How to remove the key of an array in php

How to remove the key of an array in php

PHPz
PHPzOriginal
2023-04-23 10:06:231672browse

In PHP development, array is one of the commonly used data structures. When processing an array, sometimes it is necessary to remove the key of the array and only take the value in the array. Below, we will introduce several commonly used methods to remove array keys.

Method 1: foreach loop

When using the foreach loop to traverse the array, you can only take out the value of the array. An example is as follows:

$arr = array('one'=>1, 'two'=>2, 'three'=>3);
foreach ($arr as $value) {
   echo $value . " ";
}
// 输出:1 2 3

In this way, we can only take out the values ​​​​in the array without caring about the key name.

Method 2: array_values() function

PHP provides a function array_values() that specifically removes array key names. This function returns a new array containing only all the values ​​in the array, without keys. An example is as follows:

$arr = array('one'=>1, 'two'=>2, 'three'=>3);
$newArr = array_values($arr);
print_r($newArr);
// 输出:Array ( [0] => 1 [1] => 2 [2] => 3 )

In this way, we can get a new array without key names.

Method 3: array_map() function

The array_map() function is a very powerful function. It can apply a callback function to each element in the array and then return an array. Each element of the array is the return value of the callback function. An example is as follows:

$arr = array('one'=>1, 'two'=>2, 'three'=>3);
$newArr = array_map(function($value) { return $value; }, $arr);
print_r($newArr);
// 输出:Array ( [one] => 1 [two] => 2 [three] => 3 )

As you can see, the array_map() function does not remove the key name of the array. We can achieve the effect of removing the array key name by combining the array_map() function and the array_values() function. The example is as follows:

$arr = array('one'=>1, 'two'=>2, 'three'=>3);
$newArr = array_values(array_map(function($value) { return $value; }, $arr));
print_r($newArr);
// 输出:Array ( [0] => 1 [1] => 2 [2] => 3 )

Using the array_map() function and the array_values() function together, we can get an A new array containing key names.

Method 4: Forced type conversion

Force an array to an integer or string type. PHP will automatically remove the key name of the array and only take the value in the array. An example is as follows:

$arr = array('one'=>1, 'two'=>2, 'three'=>3);
echo (int)$arr;
// 输出:Array
echo (string)$arr;
// 输出:Array

Through forced type conversion, we can also get a new array without key names.

To sum up, we have introduced four methods to remove array key names: foreach loop, array_values() function, array_map() function combined with array_values() function, and forced type conversion. Each method has its own advantages and disadvantages, and you can choose different methods according to your specific needs.

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