Home > Backend Development > PHP Problem > How to remove key (key name) from PHP one-dimensional array

How to remove key (key name) from PHP one-dimensional array

青灯夜游
Release: 2023-03-16 18:14:01
Original
2493 people have browsed it

Two removal methods: 1. Use the array_values() function to remove the key (key name), the syntax is "array_values ​​(array)". 2. Define an empty array, use the foreach statement to loop through the original array, and pass the key values ​​of the original array into the empty array in the loop body. The syntax is "foreach(original array as $v){$empty array name[]= $v;}".

How to remove key (key name) from PHP one-dimensional array

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

php one-dimensional array Two methods to remove key (key name)

Method 1: Use array_values() function to remove

array_values() function in PHP You can get the values ​​of all elements in the array. The syntax of this function is as follows:

array_values($array)
Copy after login
  • The parameter $array is the array being operated on.

array_values() function is to return the values ​​of all elements in the array. It is very simple to use. With only one required parameter, you can return an array containing all the values ​​in the given array. Array, but does not retain key names. The returned array will be in the form of an indexed array, with array indices starting at 0 and increasing by 1.

array_values() function is particularly suitable for arrays with confusing element subscripts, or for converting associative arrays into indexed arrays.

Example: array_values() function removes keys from the array

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);
$arr=array("Peter"=>65,"Harry"=>80,"John"=>78,"Clark"=>90);
var_dump($arr);
var_dump(array_values($arr));
?>
Copy after login

Output result:

How to remove key (key name) from PHP one-dimensional array

##Method 2: Use foreach Loop and remove an empty array

  • Use the foreach statement to loop through the original array

  • In the loop body, change the keys of the original array The value is passed into the empty array

  • <?php
    $arr1=array("aaa"=>11,"bbb"=>22,"ccc"=>33);
    var_dump($arr1);
    $arr2=array();
    foreach($arr1 as $v){
        $arr2[]=$v;
    }
    var_dump($arr2);
    ?>
    Copy after login

    How to remove key (key name) from PHP one-dimensional array

    Recommended learning: "

    PHP Video Tutorial"

    The above is the detailed content of How to remove key (key name) from PHP one-dimensional array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template