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

5 回答 5

4

你可以这样做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 :)

编辑:固定$newElement初始化,它必须是一个包含你的新元素的数组,所以它必须是一个包含你想要的新元素的数组,一个新的数组。

于 2012-10-31T11:30:27.377 回答
1

您可以在简单的 for 中使用 array_splice

for ($i = 0; $i < count($array); ++$i) {
    if (/* condition is true */) {
        array_splice($array, $i, 0, /* your new array */);
        ++$i;
    }
}
于 2012-10-31T11:32:06.180 回答
0

我是这样想的:

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

那里可能有一个错误,但你明白了..

于 2012-10-31T11:34:16.837 回答
0

您可以使用功能:

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;
}
于 2012-10-31T11:31:38.287 回答
0

你需要建立一个新的array

尝试

$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);

输出

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)
于 2012-10-31T11:30:41.397 回答