如何在多维数组中添加键值对而无需在php中添加索引

时间:2013-06-25 12:12:16

标签: php sugarcrm

如何在没有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
);
?>

3 个答案:

答案 0 :(得分:2)

$GLOBALS['app_list_strings']['ip_list']['10.0.0.1'] = 'server2'; 

答案 1 :(得分:0)

上面的代码是

$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' );

答案 2 :(得分:0)

您可以按照以下方式逐个添加:

$ 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'=&GT; 'server1的', '192.168.1.52'=&GT; '服务器2', '192.168.1.53'=&GT; '服务器3', '192.168.1.54'=&GT; '服务器4', '192.168.1.55'=&GT; '服务器5', '192.168.1.56'=&GT; '服务器6', '192.168.1.57'=&GT; 'server7'

);

foreach($ array_server_ips as $ key =&gt; $ value){

$GLOBALS['app_list_strings']['ip_list'][$key] = $value;

}