How can i add key value pair in multidimentional array without add index in php

人盡茶涼 提交于 2019-12-23 04:51:23

问题


How can i add key value pair in multi-dimentional array without index in php

ex:

<?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
);
?>

回答1:


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



回答2:


You can add one by one as below :

$GLOBALS['app_list_strings']['ip_list']['192.168.1.51'] = 'server1';

$GLOBALS['app_list_strings']['ip_list']['192.168.1.52'] = 'server2';..... and so on..

Or

You can use foreach to add all at a time as below :

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

}




回答3:


the code above is

$GLOBALS['app_list_strings']['ip_list']['192.168.1.51'] = 'server1';

so do this

two examples of how to add two your array

$GLOBALS['app_list_strings']['ip_list']['152.124.25.25'] = 'server x'; 
$GLOBALS['app_list_strings']['ip_list']['152.100.25.25'] = 'server r'; 

or

$GLOBALS['app_list_strings']['ip_list']=array ( '192.168.1.51' => 'server1', '152.100.25.25' => 'server x' );


来源:https://stackoverflow.com/questions/17296992/how-can-i-add-key-value-pair-in-multidimentional-array-without-add-index-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!