PHP Array Remove Keys Keep Values
Last Updated : Mar 13, 2023
IN - PHP
In this article we will show you the solution of PHP array remove keys keep values, to keep values alone and eliminate array keys you need array_values() special method.
This well known inbuilt php function helps to obtain array values from an array. This array may contain key and values pair or values only.
This function leads you to create new array to store retrieved values and assigning to numerical values.
Do to so first we need an associative array with values then pass it to array_values() help you to achieve the result easily.
Step By Step Guide On PHP Array Remove Keys Keep Values :-
You have to define one associative array with contain some values that can as much as you want. But it has to be defined as per predefined rules and display those to end user side.
After that you need to pass your array to array_values() and an variable you need to create to store this new array values to it.
Using print_r() method print it to client side and verify your result at last, where you can see key changes clearly.
<?php $arr=[ 'h'=>'Harini', 'D'=>'dhanu' ]; echo 'Before Remove Key'; print_r($arr); $r=array_values($arr); echo '<br>After Remove Key'; print_r($r); ?>
- A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
- The default file extension for php files is “.php” and php statements end with ‘;’ semicolon. In php we need to specify ‘$’ symbol before the variable name also default rule.
- First we created a array variable $arr with values ('h'=>'Harini', 'D'=>'dhanu'). Using echo statement we printing text ‘Before Remove Key Array’ and then printed array values to show users how it looks before remove keys.
- Subsequently, created array $arr passed to array_values() and returning result stored on another variable.
- This method returns array values with replaced numerical keys that’s successfully stored on variable ‘$r’.
- Once again using echo statement printed another text ‘After Remove Key Array’ to notify user like this is final result then printed array by passing result $r to print_r() method.
- Whenever you need to print complete array values must prefer print_r() otherwise you can’t get expected result.
In conclusion, hope now you know how to remove keys from associative array in php.
When you work with php we need to create and process php files at server location and then we need to start the server before execute the program.
Otherwise sometimes you will get error so ensure it if you get any error while running output.
When we execute this program on browser it will print ‘Before Remove Key Array ( [h] => Harini [D] => dhanu ) After Remove Key Array (  => Harini  => dhanu )Array (  => G  => O  => O  => D ) Array (  => G  => O  => D )’ as output.
With this help of sample code you can test with other values and you will get result according to your values change.
I hope this article on PHP array remove keys keep values helps you and the steps and method mentioned above are easy to follow and implement.