Quantcast
Channel: Eureka! » PHP
Viewing all articles
Browse latest Browse all 16

PHP – Insert and element into a specific position of an Array

$
0
0

Originally we have an array as follow

$fruits = array(
  '0' => 'apple',
  '1' => 'banana',
  '2' => 'pear',
);

 

So i want to insert “melon” into the 2nd position which is after “apple”. We can do it by array_splice().

array_splice($fruits , 1, 0, 'melon');

 

Now we have

$fruits = array(
  '0' => 'apple',
  '1' => 'melon',
  '2' => 'banana',
  '3' => 'pear',
);

 

Done =)

Reference:


Filed under: PHP Tagged: PHP

Viewing all articles
Browse latest Browse all 16

Latest Images

Trending Articles





Latest Images