[英]How can I add a $key=>$value pair to an associative array using a foreach loop in PHP?
[英]How can i add key value pair in multidimentional array without add index in php
如何在PHP中沒有索引的多維數組中添加鍵值對
例如:
<?php
$GLOBALS['app_list_strings']['ip_list']=array (
'192.168.1.51' => 'server1',
// i have to add key and value pair here just like above
);
?>
$GLOBALS['app_list_strings']['ip_list']['10.0.0.1'] = 'server2';
上面的代碼是
$GLOBALS['app_list_strings']['ip_list']['192.168.1.51'] = 'server1';
這樣做
兩個如何添加數組的例子
$GLOBALS['app_list_strings']['ip_list']['152.124.25.25'] = 'server x';
$GLOBALS['app_list_strings']['ip_list']['152.100.25.25'] = 'server r';
要么
$GLOBALS['app_list_strings']['ip_list']=array ( '192.168.1.51' => 'server1', '152.100.25.25' => 'server x' );
您可以按如下方式逐個添加:
$ GLOBALS ['app_list_strings'] ['ip_list'] ['192.168.1.51'] ='server1';
$ GLOBALS ['app_list_strings'] ['ip_list'] ['192.168.1.52'] ='server2'; .....等等..
要么
您可以使用foreach一次添加所有內容,如下所示:
$ array_server_ips = array(
'192.168.1.51'=>'server1','192.168.1.52'=>'server2','192.168.1.53'=>'server3','192.168.1.54'=>'server4','192.168.1.55'= >'server5','192.168.1.56'=>'server6','192.168.1.57'=>'server7'
);
foreach($ array_server_ips as $ key => $ value){
$GLOBALS['app_list_strings']['ip_list'][$key] = $value;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.