Jump to content

[SOLVED] re-arrange array data


sandrob57

Recommended Posts

So I have an array, technically it looks like this:

 

$data =

Array ( [0] => Array ( [gold] => 150 [food] => 23 [wood] => 27 [stone] => 11 ) [1] => Array ( [gold] => 170 [food] => 33 [wood] => 17 [stone] => 31 ) )

 

simpler terms:

 

data[0] = [gold] 50 [wood] 40 [food] 30 [stone] 25

data[1] = [gold] 30 [wood] 50 [food] 20 [stone] 65

 

So what I am trying to do is make graphs for each unit type, so:

 

GOLD

Day 1 - 50

Day 2 - 30

 

Wood

Day 1 - 40

Day 2 - 50

 

etc.

 

The only problem is, I don't know how to set these up into a foreach() by each resource category.

 

So how would i go about doing that? I tried this and just told it to get GOLD but it isnt working:

 

$income = dbquery("SELECT * FROM fusion_income WHERE user_id='".$userdata['user_id']."' ORDER BY id DESC LIMIT 0,10");

while($income_data = dbarray($income)){

$data[] = unserialize($income_data['amount']);

}

 

foreach($data[]['gold'] as $cat => $amount){

echo "$cat - $amount<br />";

}

 

The error i get is:

 

Warning: Invalid argument supplied for foreach() in /home/galava/public_html/page/graphs.php on line 43

 

sigh...  :-[

Link to comment
https://forums.phpfreaks.com/topic/61800-solved-re-arrange-array-data/
Share on other sites

Well, I finally found a way that works. Take a look if ur interested (thanks for the help, although I did find another way):

 

	$income = dbquery("SELECT * FROM fusion_income WHERE user_id='".$userdata['user_id']."' ORDER BY id DESC LIMIT 0,10");
while($income_data = dbarray($income)){
	$data[] = unserialize($income_data['amount']);
}

foreach($data as $cat => $amount){
	foreach($data[$cat] as $cat1 => $amount1){
			$supply[$cat1][] = $amount1;
	}
}

//Income Graphs
echo "<table align='center' cellpadding='0' cellspacing='1' width='1%' class='tbl-border'>";

	foreach($supply as $cat => $array){
	$supply[$cat] = array_reverse ($supply[$cat]);
	array_reverse ($supply);
	$max[$cat] = 120 / max($supply[$cat]) * 1.1;
	echo "<tr><td class='tbl2' width='1%' style='white-space: nowrap;' align='left' rowspan='2'>
				"; icon("".$cat.".gif", ""); echo "
			</td>";
		foreach($supply[$cat] as $day_number => $value){ 
			echo"
				<td class='tbl1' style='white-space: nowrap;' align='left' valign='bottom'>
				<img src='".THEME."images/pollbar.gif' height='".(($value * $max[$cat]) + 10)."' width='12' align='bottom' style='border:1px #444 solid;'>
				</td>
			";
		}

		echo "</tr><tr>";

		foreach($supply[$cat] as $day_number => $value){ 
			echo"
				<td class='tbl1' style='white-space: nowrap;' align='left' valign='bottom'>
				<span class='small2'>".$value."</span>
				</td>
			";
		}
	echo "</tr>";
	}

echo "</table>";	

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.