Page 1 of 1

Appending Array

Posted: Fri Apr 07, 2006 7:04 pm
by alexus
Hey,
So if I have double leyer array how can I add extra data from the erray with the same structure?

Ex: server[0][ip_addr] = 1.1.1.1
and I want say add another server server with Ip so nex record would be server[1][ip_addr] =2.2.2.2

How to?


Thanks!

Posted: Fri Apr 07, 2006 7:05 pm
by John Cartwright
$server[]['ip_addr'] = '2.2.2.2';

Remove the numerical indice :)

Posted: Fri Apr 07, 2006 7:07 pm
by feyd
<pet peeve> remember to quote named indices.

Posted: Fri Apr 07, 2006 7:09 pm
by alexus
ok but something makes me worrry what if i have more then one argument say
server[1][ip_addr] =2.2.2.2
server[1][port] = 88
server[1][ttl]=50

if I make this server[][ipadress] , server[][port] how would php know to add it in the same "pool"?

Posted: Fri Apr 07, 2006 7:12 pm
by feyd

Code: Select all

$foo = array(
  'ip_addr' => '2.2.2.2',
  'port' => 88,
  'ttl' => 50
);
$server[] = $foo;

Posted: Fri Apr 07, 2006 7:16 pm
by alexus
feyd, but still thats a regular assigning of stuks to an array, but I have double layer, and I need first layer to go I++ and assign evertthing else "mannually"..... eee im getting overloaded

Posted: Fri Apr 07, 2006 7:19 pm
by feyd
alexus wrote:feyd, but still thats a regular assigning of stuks to an array, but I have double layer, and I need first layer to go I++ and assign evertthing else "mannually"..... eee im getting overloaded
It does go "I++" notice the last line, that adds the newly created array to the end of $server, another array (from your previous posts in this thread.)

Posted: Fri Apr 07, 2006 7:19 pm
by John Cartwright

Code: Select all

$foo = array(
  'ip_addr' => '2.2.2.2',
  'port' => 88,
  'ttl' => 50
);

#some more processing...


$foo['someOther'] = $blah;
$foo['someOther2'] = $blah2;

#when processing is done then assign to parent array

$server[] = $foo;
If I understood correctly.. :wink:

Posted: Fri Apr 07, 2006 7:25 pm
by alexus
oh

Code: Select all

$server[] = $foo;
this is now used to append, hm ok let me try that, I always thought thta would preplace the content with the new one