gregm Posted June 4, 2012 Share Posted June 4, 2012 I'm successfully gotten on top of arrays but now I need to use a 2D array. I have the following variables which will contain strings with values retrieved from a sql database. $Server $Site $Status I want to create an array, so I will have something like this: myrecord[0] = "Name0", "Site0", "Status0" myrecord[1] = "Name1", "Site1", "Status1" myrecord[2] = "Name2", "Site2", "Status2" Can I do something like this: $myrecord = array(); $No_Of_records = (gets this value from sql) for ($i = 1; $i < $No_Of_records; $i++) { $Server = (gets a value from sql) $Site = (gets a value from sql) $Status = (gets a value from sql) $myrecord[$i] = array($Server, $Site, $Status) } Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 4, 2012 Share Posted June 4, 2012 Did you try it? Quote Link to comment Share on other sites More sharing options...
Barand Posted June 4, 2012 Share Posted June 4, 2012 easier to <?php $myArray = array(); $res = mysql_query("SELECT server,site,status FROM mytable"); while ($row = mysql_fetch_row($res)) { $myArray[] = $row; // add row array to myArray } ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 4, 2012 Share Posted June 4, 2012 Or even $myArray = array(); $res = mysql_query("SELECT server,site,status FROM mytable"); while ($myArray[] = mysql_fetch_row($res)) { } Although I don't really advise that - I think having a loop with nothing in it is just odd. But, anyway, do you even need to do this? You already have the results from the query and all this would do is take those same results and put into an array. There are some scenarios where this would make sense, but most time you can just process the results directly from the query. Quote Link to comment Share on other sites More sharing options...
gregm Posted June 5, 2012 Author Share Posted June 5, 2012 Yes- i tried it. I don't get any errors but I don't think its populating the array. Either that or I'm not printing the results correctly. I've got this to print the first record in the array - but it doesnt print anything. for ($i = 0; $i < count($myrecord) ; $i++) { echo $myrecord[0]; } I've also tried echo $myrecord[0,0]; and echo $myrecord[0,0,0]; Either my code above to fill the array is incorrect, or my code to print out the array - or both. Quote Link to comment Share on other sites More sharing options...
Barand Posted June 5, 2012 Share Posted June 5, 2012 What is your code to populate the array? Quote Link to comment Share on other sites More sharing options...
gregm Posted June 5, 2012 Author Share Posted June 5, 2012 $myrecord = array(); $No_Of_records = (gets this value from sql) for ($i = 1; $i < $No_Of_records; $i++) { $Server = (gets a value from sql) $Site = (gets a value from sql) $Status = (gets a value from sql) $myrecord[$i] = array($Server, $Site, $Status) } Quote Link to comment Share on other sites More sharing options...
Barand Posted June 5, 2012 Share Posted June 5, 2012 Tells us very little, but to check if the array is populated, use something like this for a legible output echo '<pre>' . print_r($myrecord, true) . '</pre>'; Quote Link to comment Share on other sites More sharing options...
gregm Posted June 5, 2012 Author Share Posted June 5, 2012 Ahh - thanks. That line shows me all the correct info. I guess the array is being populated correctly and I'm looking at the wrong spot My printout of the array must be at fault. Quote Link to comment Share on other sites More sharing options...
gregm Posted June 5, 2012 Author Share Posted June 5, 2012 @Barand - that line prints out: [1] => Array ( [0] => Server1 [1] => Status1 [2] => Site1 ) [2] => Array ( [0] => Server2 [1] => Status2 [2] => Site2 ) [3] => Array ( [0] => Server3 [1] => Status3 [2] => Site3 ) How in blue blazes do I print these values out. For example to print the first array: echo "Server" . $myrecord[0][0] . "Status:" . $myrecord[0][1] . "Site:" . $myrecord[0][2]; That doesnt work - am I close? Quote Link to comment Share on other sites More sharing options...
gregm Posted June 5, 2012 Author Share Posted June 5, 2012 Doh - its so clear now.... echo "Server" . $myrecord[1][0] . "Status:" . $myrecord[1][1] . "Site:" . $myrecord[1][2]; Thanks for the input - all working now Quote Link to comment 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.