Forum Moderators: coopster

Message Too Old, No Replies

Adding values to an associative array

         

benj0323

8:03 pm on Oct 23, 2004 (gmt 0)

10+ Year Member



How exactly do you go about adding values to an associatve array.

I tried this and it didnt work:

for($i=0;$i<=10;$i++)
{
$array .= array("$i" => "some value");
}

Thanks for your help!

coopster

8:45 pm on Oct 23, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



for ($i=0; $i<=10; $i++) { 
$array[$i] = "some value";
}
Arrays [php.net]

Here are some more examples. print_r() [php.net] is a nice way to see how it works...

print '<pre>'; 
$array = array(
'zero',
'one',
'two' => 'deux',
'three' => 'trois'
);
print_r($array); // see what it looks like...
$array['four'] = 'quatre';
print_r($array); // see what it looks like...
$array[] = 'two'; // PHP remembers where you left off numerically
print_r($array); // see what it looks like...
print '</pre>';