MainelyDesign.com Blog

How do I reset the keys in a PHP array?

Posted on 09/11/2009 at 02:00 pm by Kevin Wentworth
Viewed 30,466 times | 0 comments

This answer was located at CodingForums.com on 9/11/09.  Answers written by PHP6 and Fou-Lu.
I had been hunting for an answer to the elusive question of how do I reset array keys in PHP.  I finally found the perfect (and thorough) answer here. There are 3 ways to reset array keys in PHP.  All of these solutions work with numerically indexed arrays only. To recap:

PHP Function: array_values()

  1. $reset_array_keys = array_values($numerically_indexed_array);

PHP Function: array_merge()

  1. $reset_array_keys = array_merge($numerically_indexed_array);

Foreach loop (aka kindergarten method)

This was the way I was going to reset the keys, but couldn't bring myself to doing it, as the name above implies.

  1. $reset_array_keys = array();
  2. foreach($numerically_indexed_array as $value) {
  3.     $reset_array_keys[] = $value;
  4. }

Cheers,

-Kevin Wentworth


Answers written by PHP6 and Fou-Lu. See Answer #4 and #6 in context at Coding Forums.

 

Bookmark and Share

Tags for How do I reset the keys in a PHP array?

Forum Answers

Comments for this Posting

No comments. Be the first to post a reply.

Sorry, comments are closed for this posting.

Please Email Kevin if you have any questions. Thanks!

Meet Site Avenger - Hosted Content Management System

Powered By: Site Avenger | Site Production: Saco Design