Insert an Element into a PHP Array

This inserts $item at position $n in $array.

$array = array_splice($array, $n, 0, $item);

Example:

PHP array:

$array = array (
  "Classic Collections",
  "Abilene",
  "TX 79602",
  "(325) 672-0000",
  "Fabulous Finds",
  "Abilene",
  "TX 79601",
  "(325) 677-0000",
  "Honeycomb Tree",
  "Abilene",
  "TX 79601",
  "(325) 338-8000",
);

Element to insert:
$item = "4th Street and Vine";

Insert at this position:
$n = 1;

PHP:

$array = array_splice($array, $n, 0, $item);

Produces this PHP array:

Array (
  [0] => Classic Collections
  [1] => 4th Street and Vine
  [2] => Abilene 
  [3] => TX 79602
  [4] => (325) 672-0000
  [5] => Fabulous Finds
  [6] => Abilene
  [7] => TX 79601
  [8] => (325) 677-0000
  [9] => Honeycomb Tree
  [10] => Abilene
  [11] => TX 79601
  [12] => (325) 338-8000
);
Copyright © Lage.us Website Development | Disclaimer | Privacy Policy | Terms of Use