Appending Array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alexus
Forum Contributor
Posts: 159
Joined: Fri Jul 04, 2003 10:49 pm

Appending Array

Post 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!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

$server[]['ip_addr'] = '2.2.2.2';

Remove the numerical indice :)
Last edited by John Cartwright on Fri Apr 07, 2006 7:09 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

<pet peeve> remember to quote named indices.
alexus
Forum Contributor
Posts: 159
Joined: Fri Jul 04, 2003 10:49 pm

Post 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"?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$foo = array(
  'ip_addr' => '2.2.2.2',
  'port' => 88,
  'ttl' => 50
);
$server[] = $foo;
alexus
Forum Contributor
Posts: 159
Joined: Fri Jul 04, 2003 10:49 pm

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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:
alexus
Forum Contributor
Posts: 159
Joined: Fri Jul 04, 2003 10:49 pm

Post 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
Post Reply