在下面的数组中,如何将新数组推送到指定位置的$ 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"));
}
Run Code Online (Sandbox Code Playgroud)
你可以用array_splice.对于你的例子:
if($some_condition == 'met') {
// splice new option into array at position 3
array_splice($options, 3, 0, array($new_option));
}
Run Code Online (Sandbox Code Playgroud)
注意:array_splice期望最后一个参数是新元素的数组,因此对于您的示例,您需要传递包含新选项数组的数组.