Php 将数组插入到命名数组中的指定位置

Php 将数组插入到命名数组中的指定位置,php,arrays,Php,Arrays,在下面的数组中,如何将新数组推送到指定位置的$options数组中 $options = array ( array( "name" => "My Options", "type" => "title"), array( "type" => "open"), array("name" => "Test", "desc" => "Test", "id" => $shortname."_theme", "t

在下面的数组中,如何将新数组推送到指定位置的$options数组中

$options = array (
    array( "name" => "My Options",
    "type" => "title"),
    array( "type" => "open"),

    array("name" => "Test",
    "desc" => "Test",
    "id" => $shortname."_theme",
    "type" => "selectTemplate",
    "options" => $mydir ),

//I want the pushed array inserted here.

    array("name" => "Test2",
    "desc" => "Test",
    "id" => "test2",
    "type" => "test",
    "options" => $mydir ),

    array( "type" => "close")
    );

    if(someCondition=="met")
    {
    array_push($options, array( "name" => "test",
        "desc" => "description goes here",
        "id" => "testMet",
        "type" => "checkbox",
        "std" => "true"));
    }
你可以用。例如:

if($some_condition == 'met') {
  // splice new option into array at position 3
  array_splice($options, 3, 0, array($new_option));
}
注意:array_splice希望最后一个参数是新元素数组,因此对于您的示例,您需要传递一个包含新选项数组的数组。

您可以使用。例如:

if($some_condition == 'met') {
  // splice new option into array at position 3
  array_splice($options, 3, 0, array($new_option));
}
注意:array_splice希望最后一个参数是新元素数组,因此对于您的示例,您需要传递一个包含新选项数组的数组。

简单

array_splice($options, 3, 0, $newArr);
简单地

array_splice($options, 3, 0, $newArr);
如上所述,您可以使用array_splice,但不要忘记将数组包装到另一个数组中,如下所示:

如果“满足”=$some\u条件 { array_splice$选项,3,0,arrayarray '名称'=>'测试', 'desc'=>'此处有说明', 'id'=>'testMet', '键入'=>'复选框', 'std'=>'true' ; } 编辑:connec已经指定了它的响应。

如前所述,您可以使用array_splice,但不要忘记将您的数组包装到另一个数组中,如下所示:

如果“满足”=$some\u条件 { array_splice$选项,3,0,arrayarray '名称'=>'测试', 'desc'=>'此处有说明', 'id'=>'testMet', '键入'=>'复选框', 'std'=>'true' ; }
编辑:connec已指定其响应。

对于插入新数组,而不是插入到$r[4]和$r[5]之间的特定位置,请执行以下操作:

$options[]=array ("key" => "val"); //insert new array
$options[]=$v; //insert new variable
要在spesific变量后插入新数组,请执行以下操作:

function array_push(&$array,$after_element_number,$new_var)
{
  array_splice($array, $after_element_number, 0, $new_var);
}

if(someCondition=="met")
{
array_push($options, 2, array( "name" => "test",
    "desc" => "description goes here",
    "id" => "testMet",
    "type" => "checkbox",
    "std" => "true"));
}

对于插入新数组,而不是插入到$r[4]和$r[5]之间的特定位置:

$options[]=array ("key" => "val"); //insert new array
$options[]=$v; //insert new variable
要在spesific变量后插入新数组,请执行以下操作:

function array_push(&$array,$after_element_number,$new_var)
{
  array_splice($array, $after_element_number, 0, $new_var);
}

if(someCondition=="met")
{
array_push($options, 2, array( "name" => "test",
    "desc" => "description goes here",
    "id" => "testMet",
    "type" => "checkbox",
    "std" => "true"));
}