Home >Backend Development >PHP Problem >How to remove key from associative array in php

How to remove key from associative array in php

PHPz
PHPzOriginal
2023-04-20 10:11:59474browse

Associative arrays in PHP are composed of one or more key-value pairs, where each key is unique in the array, and each value is associated with a key association. Sometimes, we may need to remove the keys in the array and only keep the values. This article will introduce several methods to achieve this goal.

Method 1: Use the array_values ​​function
The array_values ​​function can return a new array containing all the values ​​in the array, without containing the original keys. The sample code is as follows:

<?php
$originalArr = array("name"=>"Tom", "age"=>30, "job"=>"Developer");
$newArr = array_values($originalArr);
print_r($newArr);
?>

The output is as follows:

Array ([0] => Tom [1] => 30 [2] => Developer)

Using the array_values ​​function, we get a new array that only contains the values ​​in the array, in which the keys have been removed.

Method 2: Use foreach loop
If we want to directly change the original array, we can use a foreach loop to traverse the array and assign each value to a new array variable. The sample code is as follows:

<?php
$originalArr = array("name"=>"Tom", "age"=>30, "job"=>"Developer");

$newArr = array();
foreach($originalArr as $value){
    $newArr[] = $value;
}
print_r($newArr);
?>

The output result is as follows:

Array ([0] => Tom [1] => 30 [2] => Developer)

Method 3: Use array_map function
array_map function can apply a callback function to each element of the array and return a Array of all results. We can simply use a callback function that returns an array value to achieve our needs. The sample code is as follows:

<?php
$originalArr = array("name"=>"Tom", "age"=>30, "job"=>"Developer");

$newArr = array_map(function($value){ return $value; }, $originalArr);
print_r($newArr);
?>

The output result is as follows:

Array ([0] => Tom [1] => 30 [2] => Developer)

Method 4: Use array_flip function
array_flip function can exchange the keys and values ​​in the array, and the original keys become values. The original value becomes the key. We can use this function to first swap the keys and values, and then use the array_values ​​function to remove the keys. The sample code is as follows:

<?php
$originalArr = array("name"=>"Tom", "age"=>30, "job"=>"Developer");

$newArr = array_flip($originalArr);
$newArr = array_values($newArr);
print_r($newArr);
?>

The output result is as follows:

Array ([0] => Tom [1] => 30 [2] => Developer)

The above are several methods for removing array keys. Different methods are suitable for different scenarios. Choose the appropriate one according to the specific situation. Methods can improve the efficiency of writing code.

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