PDA

View Full Version : Another dumb array() question...


SneakyDave
03-26-2001, 01:02 AM
OK, here's something else that I can't seem to figure out. I've read the PHP manual for examples or answers, but for some reason, nothing is working.

I have a 2 dimensional array like this (this is an example from php.net):

<?
$users = array(
&quot;djuan&quot; => array(
[nbsp][nbsp] &quot;firstname&quot; => &quot;Don&quot;,
[nbsp][nbsp] &quot;lastname&quot; => &quot;Juan&quot;,
[nbsp][nbsp] &quot;age&quot; => &quot;102&quot;,
[nbsp][nbsp] &quot;weight&quot; => &quot;100&quot;
),

[nbsp]&quot;jordan23&quot; => array(
[nbsp][nbsp][nbsp][nbsp]&quot;firstname&quot; => &quot;Michael&quot;,
[nbsp][nbsp][nbsp][nbsp]&quot;lastname&quot; => &quot;Jordan&quot;,
[nbsp][nbsp][nbsp][nbsp]&quot;age&quot; => &quot;36&quot;,
[nbsp][nbsp][nbsp][nbsp]&quot;weight&quot; => &quot;225&quot;
)
);
?>

OK, I have an 2 dimensional array called $users. I can print out what it looks like by doing this:

<?
while ( list($username, $subarray) = each($users) ) {
[nbsp][nbsp]echo &quot;Username: $username: &quot;;[nbsp][nbsp]
[nbsp][nbsp]echo &quot;<ul>&quot;;
[nbsp][nbsp]while ( list($key, $val) = each($subarray) ) {
[nbsp][nbsp][nbsp][nbsp]echo &quot; $key : $val\n&quot;;
[nbsp][nbsp]}
[nbsp]echo &quot;[/list]&quot;;
}
?>


Now, the simple problem is, how would I add more names while processing? I tried it this way:

$users = array(
&quot;sd&quot; => array(
[nbsp][nbsp] &quot;firstname&quot; => &quot;sneaky&quot;,
[nbsp][nbsp] &quot;lastname&quot; => &quot;dave&quot;,
[nbsp][nbsp] &quot;age&quot; => &quot;666&quot;,
[nbsp][nbsp] &quot;weight&quot; => &quot;200&quot;
),


But that doesn't work of course because it re-initializes the array. I don't see how you'd insert more values into the table other than at initialization. Would it look something like this?

$users[&quot;sd&quot;][&quot;firstname&quot;] = &quot;sneaky&quot;;
$users[&quot;sd&quot;][&quot;lastname&quot;] = &quot;dave&quot;;
$users[&quot;sd&quot;][&quot;age&quot;] = &quot;666&quot;;
$users[&quot;sd&quot;][&quot;weight&quot;] = &quot;200&quot;;


[nbsp]

SneakyDave
03-26-2001, 01:05 AM
Geesh! Stupid me, if I would have spent the time and typed what I used above, it would have worked! I had an error in my code where I was adding something like this:
$users[&quot;sd&quot;] = &quot;sd&quot;;

Which must have caused all my problems.