我有来自循环 foreach 的数组多维。 我想在数组多维末尾的数组中插入新数据,如下所示:
Array
(
[setting_code] => gen_logo
[setting_value] => logo.png
)
Array
(
[0] => Array
(
[setting_code] => gen_site_name
[setting_value] => Codeigniter School CMS
)
[1] => Array
(
[setting_code] => gen_email_info
[setting_value] => info@schoolapp.sch
)
[2] => Array
(
[setting_code] => gen_meta_author
[setting_value] => awnLabs.co
)
)
然后我想要的结果如下:
Array
(
[0] => Array
(
[setting_code] => gen_site_name
[setting_value] => Codeigniter School CMS
)
[1] => Array
(
[setting_code] => gen_email_info
[setting_value] => info@schoolapp.sch
)
[2] => Array
(
[setting_code] => gen_meta_author
[setting_value] => awnLabs.co
)
[3] =>Array
(
[setting_code] => gen_logo
[setting_value] => logo.png
)
)
如何得到如上所述的结果?感谢以前的帮助:)
答案 0 :(得分:1)
使用array_push在数组末尾插入数据。
答案 1 :(得分:1)
只需使用$arr2[] = $arr1
或array_push()
进行分配即可。例如:
$arr1 = Array
(
"setting_code" => "gen_logo",
"setting_value" => "logo.png",
);
$arr2 = array
(
0 => array
(
"setting_code" => "gen_site_name",
"setting_value" => "Codeigniter School CMS"
),
1 => array
(
"setting_code" => "gen_email_info",
"setting_value" => "info@schoolapp.sch"
),
2 => array
(
"setting_code" => "gen_meta_author",
"setting_value" => "awnLabs.co",
)
);
$arr2[] = $arr1;
// or
//array_push($arr2, $arr1);
print "<pre>";
print_r($arr2);
print "</pre>";
答案 2 :(得分:0)
您应该使用array_merge()
功能。
以下是示例:
$arr1 =
Array
(
"setting_code" => "gen_logo",
"setting_value" => "logo.png",
);
$arr2 =
Array
(
Array
(
"setting_code" => "gen_site_name",
"setting_value" => "Codeigniter School CMS"
),
//Rest elements
);
$result = array_merge($arr2, $arr1);
答案 3 :(得分:0)
array_push简单易行。例如:
$logo_1 = array
(
'setting_code' => 'gen_logo',
'setting_value' => 'logo.png'
);
$logo_2 = array
(
'setting_code' => 'gen_logo',
'setting_value' => 'logo.png'
);
$arr = array();
array_push($arr, $logo_1);
array_push($arr, $logo_2);
var_dump($arr);
答案 4 :(得分:0)
$var=array();
$test_arr['setting_code']='gen_site_name';
$test_arr['setting_value']='Codeigniter School CMS';
array_push($var, $test_arr);
$test_arr['setting_code']='gen_email_info';
$test_arr['setting_value']='info@schoolapp.sch';
array_push($var, $test_arr);
$test_arr['setting_code']='gen_meta_author';
$test_arr['setting_value']='awnLabs.co';
array_push($var, $test_arr);
foreach ($var as $key=>$value){
$arr[$key]=$value;
}
通过使用此代码,您将获得所需的输出,这是array_push
的一个小例子由于