如何在没有索引的多维数组中添加键值对
前任:
<?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']['192.168.1.52'] = 'server2';..... 等等..
或者
您可以使用 foreach 一次添加所有内容,如下所示:
$array_server_ips = 数组(
'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;
}
上面的代码是
$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' );