Jump to content

[SOLVED] How To array_push multidimensional arrays


Zeradin

Recommended Posts

I can't find the answer anywhere. Seems so simple. Here's what I want to do:

0=>SAT=>1600,1=>GMAT=>1150

from

	$sql = "
			SELECT 
				*
			FROM	
				test_score
			JOIN
				test_type
			ON
				test_score.test_type_id = test_type.test_type_id
			WHERE 
				user_id = '$this->user_id'
			";

	//echo $sql;
	$result = $this->query($sql);

	$test_array = array();

	if(mysql_num_rows($result) > 0)
	{
		while($row = mysql_fetch_array($result))
		{
			$name = $row['test_type_name'];
			$score = $row['score'];
			array_push($test_array,[$name][$score]);
		}
	}
	else
	{
		return false;
	}
	//print_r($test_array);
	return $test_array;

 

which obviously doesn't work. Thanks!

according to php.net

 

 

"

 

Array_push also works fine with multidimensional arrays. Just make sure the element is defined as an array first.

 


$array["element"][$element]["element"] = array();
array_push ($array["element"][$element]["element"], "banana");


 

http://us3.php.net/array_push

 

 

according to php.net

"

Array_push also works fine with multidimensional arrays. Just make sure the element is defined as an array first.

 

 

$array["element"][$element]["element"] = array();

array_push ($array["element"][$element]["element"], "banana");

?>

 

[/php

 

http://us3.php.net/array_push

 

?

 

Yes, I saw this... unfortunately it doesn't make any sense to me. They're array_push-ing one value to a three dimensional array, one of which is a variable and two of which have the same name. I've been trying for like an hour to get that to work. I need someone to explain something new to me.

new idea

 

	$test_array = array();

	if(mysql_num_rows($result) > 0)
	{
		while($row = mysql_fetch_array($result))
		{
			$name = $row['test_type_name'];
			$score = $row['score'];
			array_push($test_array, $name => $score);
		}
	}

 

gets a "Parse error: syntax error, unexpected T_DOUBLE_ARROW in /home/storm/public_html/smart/admin/classes/Test.class.php on line 112"

 

why?

I solved the problem finally... here's what works in case anyone is trying to solve this problem in the future:

 

		$test_array = array();

		if(mysql_num_rows($result) > 0)
		{
			while($row = mysql_fetch_array($result))
			{
				$name = $row['test_type_name'];
				$score = $row['score'];
				$test_array[] = array($name => $score);
			}
		}

I was wondering if I didn't know the key... like SAT could be anything. I finally got it solved with:

$x = 0;
			//print_r($expert_test);
			//echo sizeof($expert_test);
			while($x < sizeof($expert_test))
			{
				foreach ($expert_test[$x] as $key => $row)
				{
					echo '
					<div id="expert_'.$key.'" class="expert_width">
						<span id="expert_'.$key.'_title" class="expert_detail">'.$key.'</span>
						<span id="expert_gmat_score" class="expert_detail">'.$row.'</span>
						<span id="expert_gmat_ssb" class="expert_detail">#4</span>
					</div>';
					$x++;
				} 
			}?>

Thanks!

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.