Zeradin Posted July 23, 2009 Share Posted July 23, 2009 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 https://forums.phpfreaks.com/topic/167172-solved-how-to-array_push-multidimensional-arrays/ Share on other sites More sharing options...
seventheyejosh Posted July 23, 2009 Share Posted July 23, 2009 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 Link to comment https://forums.phpfreaks.com/topic/167172-solved-how-to-array_push-multidimensional-arrays/#findComment-881447 Share on other sites More sharing options...
Zeradin Posted July 23, 2009 Author Share Posted July 23, 2009 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 https://forums.phpfreaks.com/topic/167172-solved-how-to-array_push-multidimensional-arrays/#findComment-881448 Share on other sites More sharing options...
Zeradin Posted July 23, 2009 Author Share Posted July 23, 2009 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 https://forums.phpfreaks.com/topic/167172-solved-how-to-array_push-multidimensional-arrays/#findComment-881467 Share on other sites More sharing options...
Zeradin Posted July 23, 2009 Author Share Posted July 23, 2009 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 https://forums.phpfreaks.com/topic/167172-solved-how-to-array_push-multidimensional-arrays/#findComment-881472 Share on other sites More sharing options...
Zeradin Posted July 23, 2009 Author Share Posted July 23, 2009 Actually... I need help reading out of it... how do i get this data out? Array ( [0] => Array ( [sAT] => 1600 ) [1] => Array ( [MCAT] => 1150 ) ) Link to comment https://forums.phpfreaks.com/topic/167172-solved-how-to-array_push-multidimensional-arrays/#findComment-881483 Share on other sites More sharing options...
Philip Posted July 23, 2009 Share Posted July 23, 2009 Just how you originally stated it in the beginning $array[0]['SAT'] or $array[1]['MCAT'] Link to comment https://forums.phpfreaks.com/topic/167172-solved-how-to-array_push-multidimensional-arrays/#findComment-881488 Share on other sites More sharing options...
Zeradin Posted July 23, 2009 Author Share Posted July 23, 2009 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 https://forums.phpfreaks.com/topic/167172-solved-how-to-array_push-multidimensional-arrays/#findComment-881490 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.