简体   繁体   中英

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

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
);
?>
$GLOBALS['app_list_strings']['ip_list']['10.0.0.1'] = 'server2'; 

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

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;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM