tags:

views:

24

answers:

1

I need to create a new line of data within an array where the line number = a given number

pseudo code

$info = array("Breakfast", "Lunch", "Dinner");
$target = "1"; //define where i want new data, pushing other data down
$inject = "Brunch";

$newarray = somefunction($info, $target, $inject);

$newarray now looks like

[0]Breakfast
[1]Brunch
[2]Lunch
[3]Dinner
+2  A: 

You can use the array_splice function to do so:

array_splice($info, $target, 0, $inject);

But note that array_splice modifies the original array. So you would need to copy the array first and operate on the copy:

$newarray = $info;
array_splice($newarray, $target, 0, $inject);
Gumbo