How to add elements to the beginning of an array in PHP

Topic: PHP / MySQL« Prev|Next »

Answer: Use the PHP array_unshift() function

You can use the PHP array_unshift() function to insert one or more elements or values to the beginning of an array. Here's an example.

  • <?php
  • $skills = array("HTML5", "CSS3", "JavaScript");
  •  
  • // Prepend array with new items
  • array_unshift($skills, "Illustrator", "Photoshop");
  • print_r($skills);
  • ?>

Related FAQ

Here are some more FAQ related to this topic:

 
Close

Your Feedback:

 

We would love to hear from you! Please say something.