0

Here is my example array

$inputs = array();

array(4) {
  [0]=>
  array(2) {
    ["id"]=>
    string(1) "1"
    ["name"]=>
    string(4) "13.1"
  }
  [1]=>
  array(2) {
    ["id"]=>
    string(1) "2"
    ["name"]=>
    string(4) "13.2"
  }
  [2]=>
  array(2) {
    ["id"]=>
    string(1) "3"
    ["name"]=>
    string(4) "14.1"
  }
  [3]=>
  array(2) {
    ["id"]=>
    string(1) "4"
    ["name"]=>
    string(4) "14.2"
  }
}

During my foreach loop, I could be currently working with $inputs[2] and conditional logic would determine that immediately above this one a new array needs to be added. Please see next example..

array(4) {
  [0]=>
  array(2) {
    ["id"]=>
    string(1) "1"
    ["name"]=>
    string(4) "13.1"
  }
  [1]=>
  array(2) {
    ["id"]=>
    string(1) "2"
    ["name"]=>
    string(4) "13.2"
  }

  [2]=>
  array(2) {
    ["id"]=>
    string(1) "NEW"
    ["name"]=>
    string(4) "NEW"
  }

  [3]=>
  array(2) {
    ["id"]=>
    string(1) "3"
    ["name"]=>
    string(4) "14.1"
  }
  [4]=>
  array(2) {
    ["id"]=>
    string(1) "4"
    ["name"]=>
    string(4) "14.2"
  }
}

How would I do this?

thanks for your help in advance

4
  • 1
    Please try to clarify what you want to achieve, it's not clear to me. Commented Oct 31, 2012 at 11:26
  • @Nelson Hi I'm running through a foreach loop and a function is called to add an 'entry' above the current 'active' key. I don't know how else to explain it. I'd rather that is is inserted immediately above, rather than top or bottom to maintain ordering. Commented Oct 31, 2012 at 11:30
  • Is this like insertion sort? As in, do you only need to insert one item each time? Commented Oct 31, 2012 at 11:57
  • @Jack yes, just needs to be added at the time the function determines that it needs to happen. Commented Oct 31, 2012 at 12:05

5 Answers 5

4

You can do this with array_splice:

$inputs = ... your original array;
$newElement = array(array('id' => 'NEW', 'name' => 'NEW'));
array_splice($inputs, 2, 0, $newElement);

var_dump($inputs); // your desired array :)

edit: fixed $newElement initialization, it must be an array containing your new elements, so it must be an array that contains the new element you want, a new array.

Sign up to request clarification or add additional context in comments.

1 Comment

@Jack inside a for it should work. Inside a foreach I don't know, but I guess it won't because the internal pointer of the array may get modified by array_splice
1

You can use array_splice in a simple for

for ($i = 0; $i < count($array); ++$i) {
    if (/* condition is true */) {
        array_splice($array, $i, 0, /* your new array */);
        ++$i;
    }
}

Comments

0

You can use function:

function insertIn($array, $position, $element)
{
    $output = array();
    for($i=0;$i<$position;$i++)
    {
         $output[$i] = $array[$i];
    }
    $output[$position] = $element;
    for($i=$position+1;$i<count($array)+1;$i++)
    {
         $output[$i] = $array[$i-1];
    }
    return $output;
}

Comments

0

I think this way:

$new_array= array_merge( array_slice($array, 0, $pos), array(($pos+1)=>$value), array_slice($array, $pos, count($array)-$pos) );

There might be a bug in there, but you get the idea ..

Comments

0

You would need to build a new array

try

$array = array(
        0 => array("id" => "1","name" => "13.1"),
        1 => array("id" => "2","name" => "13.2"),
        2 => array("id" => "3","name" => "14.1"),
        3 => array("id" => "4","name" => "14.2"));


$append = array("id" => "new","name" => "new");
$copy = array();
$id = 2 ; // add after this ID

foreach($array as $value)
{
    $copy[] = $value ;
    $id == $value['id'] AND $copy[] = $append ;

}

var_dump($copy);

Output

array
  0 => 
    array
      'id' => string '1' (length=1)
      'name' => string '13.1' (length=4)
  1 => 
    array
      'id' => string '2' (length=1)
      'name' => string '13.2' (length=4)
  2 => 
    array
      'id' => string 'new' (length=3)
      'name' => string 'new' (length=3)
  3 => 
    array
      'id' => string '3' (length=1)
      'name' => string '14.1' (length=4)
  4 => 
    array
      'id' => string '4' (length=1)
      'name' => string '14.2' (length=4)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.