Jump to content

Archived

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

Zeradin

[SOLVED] How To array_push multidimensional arrays

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!

Share this post


Link to post
Share on other sites

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

 

 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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);
			}
		}

Share this post


Link to post
Share on other sites

Actually... I need help reading out of it... how do i get this data out?

 

Array ( [0] => Array ( [sAT] => 1600 ) [1] => Array ( [MCAT] => 1150 ) )

Share this post


Link to post
Share on other sites

Just how you originally stated it in the beginning

$array[0]['SAT'] or $array[1]['MCAT']

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

×
×
  • 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.