foreach w/nested variable-depth multi-dimensional arrays

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
Saxywolf
Forum Newbie
Posts: 5
Joined: Thu Aug 28, 2003 8:51 pm

foreach w/nested variable-depth multi-dimensional arrays

Post by Saxywolf »

I want to display all the information for 10 ppl. This is what I tried:

Code: Select all

$Player[1]['Name'] = "Joe";
$Player[1]['Rank'] = "Newbie";
$Player[1]['Skills']['Digging'] = 0;
for ($Player_Num = 1; $Player_Num <= 10; $Player++)
{
	echo "$Player_Num : {$Player[$Player_Num]['Name']}<br>";
	echo $Player[$Player_Num]['Rank'] . '<br>';
	foreach ($Player[$Player_Num]['Skills'] as $Skills)
		foreach ($Skills as $Skill)
			echo "$Skills : $Skill<br>";
}
Output should be:
Joe
Newbie
Digging : 0

it seems I'm not allowed to put:
$Player[$Player_Num]['Skills']
in the foreach... any suggestions
Last edited by Saxywolf on Thu Sep 04, 2003 8:07 pm, edited 1 time in total.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

i'm not sure if you made a mistake in your fontifying this line or not,
echo "$Player_Num :[/color {$Player[$Player_Num]['Name']}<br>";
but i figure you dont know about the <php></php> tag they have here. use that instead. it's much easier and you can copy and paste your code. only swith < to [ and > to ].

here' swhat that (assumeing there's no mistakes) llooks like with the tags:

Code: Select all

$Player[1]['Name'] = "Joe"; 
$Player[1]['Rank'] = "Newbie"; 
$Player[1]['Skills']['Digging'] = 0; 
for ($Player_Num = 1; $Player_Num <= 10; $Player++) 
{ 
echo "$Player_Num :[/color {[color=blue]$Player[$Player_Num]['Name']}<br>"; 
echo $Player[$Player_Num]['Rank'] . '<br>'; 
foreach ($Player[$Player_Num]['Skills'] as $Skills) 
foreach ($Skills as $Skill) 
echo "$Skills : $Skill<br>"; 
}
the error you're getting is because you are giving the wrong thing to foreach. you also can't use arrays directly in quoted strings like that. i know there's a way but can't think of it. i assume this is a top ten, so...

Code: Select all

$ranklist='<ol>'; // make a string you can echo out later that has all the top ten
$findpeople=mysql_query(/*select statement*/); // find the top ten
$ppl=mysql_num_rows($findpeople); /// how many did we get?
if($ppl<11){ // less than 11 ppl // there's no more than 10
  for($i=0;$i<ppl;$i++){ // for each person
    $getperson=mysql_fetch_array($findpeople); // get the person
    $person=$getperson['name']; $playernum=$getperson['num'];  // get the person's info
    $rank=$getperson['rank']; $skills=$getperson['skills'];  // get the person's info
    $ranklist.="<li>Player: $person<br />Rank: $rank<br />skills: skill->sill level<ul>"; // set the easy into into the ranklist
    foreach($skills as $skill=>$skilllevel){ // for each skill (key) and skilllevel (value)
     $ranklist.="<li>$skill -> $skilllevel</li>"; // add the skill and value to the rank list
    }
  }
}else{ // there's more than ten
  for($i=0;$i<10;$i++){ // for each person
    $getperson=mysql_fetch_array($findpeople); // get the person
    $person=$getperson['name']; $playernum=$getperson['num'];  // get the person's info
    $rank=$getperson['rank']; $skills=$getperson['skills'];  // get the person's info
    $ranklist.="<li>Player: $person<br />Rank: $rank<br />skills: skill->sill level<ul>"; // set the easy into into the ranklist
    foreach($skills as $skill=>$skilllevel){ // for each skill (key) and skilllevel (value)
     $ranklist.="<li>$skill -> $skilllevel</li>"; // add the skill and value to the rank list
    }
  }
}
you should try the search feature at php.net. it have better write up on functions than books, as well as more current ones since it's php's home.

for more info on foreach loops, search php.net


you did good there, there's just a number of small things including the foreach issue that you should check php.net on. generally type issues and how to get around them are there.

for the most part that's all i've fixed. if you need me to explain something, just let me know
Saxywolf
Forum Newbie
Posts: 5
Joined: Thu Aug 28, 2003 8:51 pm

Post by Saxywolf »

and here I thought ppl took the time to [color] their code...

There can only be ten (maximum), numbered 1-10. I made up this array construction, but it should work with the code I got to work

Code: Select all

$player = array(
	'Name' => 'Joe',
	'Clan' => 'USA';
	'Rank1' => 'Newbie',
	'Rank2' => 'Sorcerer';
	'Skills' => array('Digging', 'Climbing')
	);
$players[1] = $player;

	echo "<table width="100%" border="2">";
	for ($Squad_Num=1; $Squad_Num<=10; $Squad_Num++)
	{
		if ($Squad_Num==1 OR $Squad_Num==6) echo "<tr>";
		echo "<td width="20%" valign=top>";
		echo "<h3>$Squad_Num : {$Player[$Squad_Num]['Name']}</h3>";
		echo "<h5>{$Player[$Squad_Num]['Clan']}</h5>";
		echo "<h4>{$Player[$Squad_Num]['Rank1']} / {$Player[$Squad_Num]['Rank2']}</h4>";

		$a = $Player[$Squad_Num]['Skills'];
		foreach ($a as $Skill)
		{
			echo "$Skill<br />";
		}
		echo "</td>";
		if ($Squad_Num==5 OR $Squad_Num==10) echo "</tr>";
	}
	echo "</table>";
1 : Joe
USA
Newbie/Sorcerer
Digging
Climbing

oh, and the skills don't actually have values. It's just wether you have them or not (in my case anyway).
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

I spotted a couple of parse errors in the code above and a few other bits:

Code: Select all

<?php
$players[1] = array(
	'Name' => 'Joe',
	'Clan' => 'USA',
	'Rank1' => 'Newbie',
	'Rank2' => 'Sorcerer',
	'Skills' => array('Digging', 'Climbing')
);

echo '<table width="100%" border="2">';

for ($Squad_Num = 1; $Squad_Num <= 10; $Squad_Num++) {
	
	if ($Squad_Num == 1 || $Squad_Num == 6) {
		echo '<tr>';
	}

	echo '<td width="20%" valign="top">';

	// do a bit of testing to make sure there are 10 people in the 
	// players array
	if (isset($players[$Squad_Num])) {

		// for accessiblity reasons it would be a good idea to use CSS
		// instead of header tags to change font sizes.
		echo '<h3>'.$Squad_Num.' : '.$players[$Squad_Num]['Name'].'</h3>';
		echo '<h5>'.$players[$Squad_Num]['Clan'].'</h5>';
		echo '<h4>'.$players[$Squad_Num]['Rank1'].' / '.$players[$Squad_Num]['Rank2'].'</h4>';
			
		$skills = $players[$Squad_Num]['Skills'];
	
		foreach ($skills as $skill) {
			echo $skill.'<br />';
		}
		echo '</td>';

	} else {
		echo '&nbsp;';
	}

	if ($Squad_Num == 5 || $Squad_Num == 10) {
		echo '</tr>';
	}
}

echo '</table>';

?>
Mac
Post Reply