Home > Backend Development > PHP Problem > Remove the same subscripts from php arrays (three methods)

Remove the same subscripts from php arrays (three methods)

PHPz
Release: 2023-04-05 15:06:02
Original
582 people have browsed it

In PHP programming, we often encounter situations where we need to remove the same subscripts in an array, but we don’t know how to achieve it. In this article, we will share some practical methods to remove identical subscripts from arrays in PHP.

  1. Using the array_unique() function
    The array_unique() function is one of PHP's built-in functions and can be used to remove identical elements from an array. But it cannot be used directly to remove identical subscripts. Therefore, we can exchange the key name and key value and then use array_unique() to remove the same key name.

Sample code:

<?php
$arr = array("a"=>1, "b"=>2, "c"=>2, "d"=>1);
$new_arr = array_unique(array_flip($arr));
print_r($new_arr);
?>
Copy after login

Execution result:

Array
(
    [1] => a
    [2] => b
)
Copy after login
  1. Use array_keys() function and array_flip() function
    array_keys() function can Returns all the key names in the array, and the array_flip() function can interchange the key names and values ​​in the array. Therefore, using these two functions together, we can achieve the function of deleting the same key name.

Sample code:

<?php
$arr = array("a"=>1, "b"=>2, "c"=>2, "d"=>1);
$new_arr = array_flip(array_keys(array_flip($arr)));
print_r($new_arr);
?>
Copy after login

Execution result:

Array
(
    [a] => 1
    [b] => 2
)
Copy after login
Copy after login
  1. Use foreach() loop
    Use foreach() loop to traverse the array, each The key names of the elements are stored in a new array, and it is determined whether the current key name already exists in the new array. If not, the key name and key value are added to the new array.

Sample code:

<?php
$arr = array("a"=>1, "b"=>2, "c"=>2, "d"=>1);
$new_arr = array();
foreach($arr as $key=>$value){
    if(!in_array($key,$new_arr)){
        $new_arr[$key] = $value;
    }
}
print_r($new_arr);
?>
Copy after login

Execution result:

Array
(
    [a] => 1
    [b] => 2
)
Copy after login
Copy after login

To sum up, this article introduces three methods to remove the same subscripts in PHP arrays. The scenarios used and the effects to be achieved are different, so you can choose the corresponding method according to the actual situation.

The above is the detailed content of Remove the same subscripts from php arrays (three methods). For more information, please follow other related articles on the PHP Chinese website!

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