All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

PHP Array Remove Keys Keep Values

Last Updated : Jul 1, 2023

PHP Array Remove Keys Keep Values

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.

echo 'Before Remove Key';
echo '<br>After Remove Key';
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. 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.
  3. 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.
  4. Subsequently, created array $arr passed to array_values() and returning result stored on another variable.
  5. This method returns array values with replaced numerical keys that’s successfully stored on variable ‘$r’.
  6. 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.
  7. Whenever you need to print complete array values must prefer print_r() otherwise you can’t get expected result.

Conclusion :-

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 ( [0] => Harini [1] => dhanu )Array ( [0] => G [1] => O [2] => O [3] => D ) Array ( [0] => G [2] => O [3] => 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.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪