winco7 Posted April 13, 2015 Share Posted April 13, 2015 I's trying to transform data from mysql into something like $books below but it doenst seem to be working $books = array( "phil" => array("my girl" => 2.5, "the god delusion" => 3.5, "tweak" => 3, "the shack" => 4, "the birds in my life" => 2.5, "new moon" => 3.5) ) this is how I tried doing: $sql = "SELECT * from rating where user_id=11 limit 5"; $db_result = mysql_db_query($dbname,$sql) or trigger_error(mysql_error()); $num_rows = mysql_num_rows($db_result) or trigger_error(mysql_error()); while ($row = mysql_fetch_array($db_result)) { $one = $row['bookId']; $two = $row['user_id']; $three = $row['rating']; $ArraY= array( $two => array($one=>$three) ); print_r($ArraY); } but this is what i get : Array ( [11] => Array ( [123715] => 5 ) ) Array ( [11] => Array ( [140329] => 5 ) ) Array ( [11] => Array ( [3083854] => 4 ) ) Array ( [11] => Array ( [871236761] => 1 ) ) Array ( [11] => Array ( [451179757] => 1 ) ) Array ( [11] => Array ( [451403886] => 3 ) ) Array ( [24] => Array ( [044661095X] => 4 ) ) Array ( [24] => Array ( [014010268X] => 1 ) ) Array ( [24] => Array ( [812576063] => 5 ) ) Array ( [24] => Array ( [038076654X] => 1 ) ) instead of something like: Array ( [phil] => Array ( [my girl] => 2.5 [the god delusion] => 3.5 [tweak] => 3 [the shack] => 4 [the birds in my life] => 2.5 [new moon] => 3.5 ) [sameer] => Array ( [the last lecture] => 2.5 [the god delusion] => 3.5 [the noble wilds] => 3 [the shack] => 3.5 [the birds in my life] => 2.5 [new moon] => 1 ) [john] => Array ( [a thousand splendid suns] => 5 [the secret] => 3.5 [tweak] => 1 ) [peter] => Array ( [chaos] => 5 => 3.5 ) [jill] => Array ( [the last lecture] => 1.5 [the secret] => 2.5 [the noble wilds] => 4 [the host: a novel] => 3.5 [the world without end] => 2.5 [new moon] => 3.5 ) [bruce] => Array ( [the last lecture] => 3 [the hollow] => 1.5 [the noble wilds] => 3 [the shack] => 3.5 [the appeal] => 2 [new moon] => 3 ) [tom] => Array ( [chaos] => 2.5 ) ) Any help with how i can transform that will be much appreciated THANKS Quote Link to comment https://forums.phpfreaks.com/topic/295520-mysql-output-into-php-arrary/ Share on other sites More sharing options...
Barand Posted April 13, 2015 Share Posted April 13, 2015 Do you want us to use our crystal balls and tell you which column names you should be using instead of the ones you are currently storing in the array? Quote Link to comment https://forums.phpfreaks.com/topic/295520-mysql-output-into-php-arrary/#findComment-1508957 Share on other sites More sharing options...
CroNiX Posted April 13, 2015 Share Posted April 13, 2015 (edited) This might work: $new_array = array(); while ($row = mysql_fetch_array($db_result)) { //grab the name column from the array $name = $row['name_column']; //unset the name column from the original array unset($row['name_column']; //create a new key using the name column and set the value to the $row $new_array[$name] = $row; } print_r($new_array); Edited April 13, 2015 by CroNiX Quote Link to comment https://forums.phpfreaks.com/topic/295520-mysql-output-into-php-arrary/#findComment-1508959 Share on other sites More sharing options...
winco7 Posted April 13, 2015 Author Share Posted April 13, 2015 This might work: $new_array = array(); while ($row = mysql_fetch_array($db_result)) { //grab the name column from the array $name = $row['name_column']; //unset the name column from the original array unset($row['name_column']; //create a new key using the name column and set the value to the $row $new_array[$name] = $row; } print_r($new_array); thank you so much getting there gradually. what I am aiming at really is : for each user, get the book, rating and put in different arrays like this: Array ( [phil] => Array ( [my girl] => 2.5 [the god delusion] => 3.5 [tweak] => 3 [the shack] => 4 [the birds in my life] => 2.5 [new moon] => 3.5 ) [sameer] => Array ( [the last lecture] => 2.5 [the god delusion] => 3.5 [the noble wilds] => 3 [the shack] => 3.5 [the birds in my life] => 2.5 [new moon] => 1 ) [john] => Array ( [a thousand splendid suns] => 5 [the secret] => 3.5 [tweak] => 1 ) ) Thanks Quote Link to comment https://forums.phpfreaks.com/topic/295520-mysql-output-into-php-arrary/#findComment-1508963 Share on other sites More sharing options...
ginerjm Posted April 13, 2015 Share Posted April 13, 2015 Actually you want to put the info into "different array elements", not different arrays. In your example code originally you were creating a new array each pass thru your loop. You should have used $ArrayY[] instead of just $ArrayY. Quote Link to comment https://forums.phpfreaks.com/topic/295520-mysql-output-into-php-arrary/#findComment-1508967 Share on other sites More sharing options...
winco7 Posted April 14, 2015 Author Share Posted April 14, 2015 Actually you want to put the info into "different array elements", not different arrays. In your example code originally you were creating a new array each pass thru your loop. You should have used $ArrayY[] instead of just $ArrayY. Thank you very much. that really helped unto a certain point. I've been able to achieve this: Array ( [11] => Array ( [123715] => 5 [140329] => 5 [3083854] => 4 [871236761] => 1 [451179757] => 1 [451403886] => 3 ) [24] => Array ( [123715] => 5 [140329] => 5 [3083854] => 4 [871236761] => 1 [451179757] => 1 [451403886] => 3 [044661095X] => 4 [014010268X] => 1 [812576063] => 5 [038076654X] => 1 ) [12] => Array ( [123715] => 5 [140329] => 5 [3083854] => 4 [871236761] => 1 [451179757] => 1 [451403886] => 3 [044661095X] => 4 [014010268X] => 1 [812576063] => 5 [038076654X] => 1 [4428] => 5 [435477] => 5 [4489] => 2 [843945249] => 5 [743524772] => 1 ) [30] => Array ( [123715] => 5 [140329] => 5 [3083854] => 4 [871236761] => 1 [451179757] => 1 [451403886] => 3 [044661095X] => 4 [014010268X] => 1 [812576063] => 5 [038076654X] => 1 [4428] => 5 [435477] => 5 [4489] => 2 [843945249] => 5 [743524772] => 1 [078601556X] => 1 [1418413933] => 3 [786930616] => 5 [553159232] => 1 [8441406243] => 5 ) ) whereby for each user it shows a number of books and their rating, however at the moment those books are repeated and increase n times. instead of have each user's books and ratings separate from others. this is how i'm doing it: $one=array(); while ($row = mysql_fetch_array($db_result)) { $two = $row['user_id']; $one = $row['bookId']; $three[$one] = $row['rating']; unset($row['bookId']); unset($row['user_id']); $ArraY[$two] = $three; } print_r($ArraY); thanks for your continued assistance Quote Link to comment https://forums.phpfreaks.com/topic/295520-mysql-output-into-php-arrary/#findComment-1509010 Share on other sites More sharing options...
ginerjm Posted April 14, 2015 Share Posted April 14, 2015 Why do you unset those two row elements? They are not a bother to you. As for the element creation you didn't follow my instructions. You LITERALLY s/b using $ArrayY[], not $ArraY[$two]. Your method is populating the element with index of whatever $two contains, not a new element for each book id. And what is this use of those meaningless $one,$two,$three vars? What are they? And why is $three an array itself? Quote Link to comment https://forums.phpfreaks.com/topic/295520-mysql-output-into-php-arrary/#findComment-1509025 Share on other sites More sharing options...
winco7 Posted April 14, 2015 Author Share Posted April 14, 2015 Why do you unset those two row elements? They are not a bother to you. As for the element creation you didn't follow my instructions. You LITERALLY s/b using $ArrayY[], not $ArraY[$two]. Your method is populating the element with index of whatever $two contains, not a new element for each book id. And what is this use of those meaningless $one,$two,$three vars? What are they? And why is $three an array itself? got it working now: while ($row = mysql_fetch_array($db_result)) { $userID = $row['user_id']; #users $bookID = $row['title']; #bookTitle $rating = $row['rating']; $ArraY[$userID][$bookID] = $rating; } thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/295520-mysql-output-into-php-arrary/#findComment-1509040 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.