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!

Link to comment
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.

Link to comment
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?

Link to comment
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);
			}
		}

Link to comment
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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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