Solarpitch Posted December 25, 2012 Share Posted December 25, 2012 (edited) Hey Guys, I'm after creating an array of some description and cant seem to figure out how to get the data from it: The array looks like this... Array ( [1] => Array ( [0] => stdClass Object ( [id] => 1 [question] => Sprint Planning duration: between 2-4 hours [question_category] => 1 [created] => [category_name] => Sprint Planning [question_id] => 1 ) [1] => stdClass Object ( [id] => 1 [question] => Team used Roman Voting to agree what user stories will be part of the sprint [question_category] => 1 [created] => [category_name] => Sprint Planning [question_id] => 2 ) [2] => stdClass Object ( [id] => 1 [question] => Backlog is correctly prioritized [question_category] => 1 [created] => [category_name] => Sprint Planning [question_id] => 3 ) [3] => stdClass Object ( [id] => 1 [question] => Team discuss user story and acceptance criteria for each story and have clear understanding of what is expected [question_category] => 1 [created] => [category_name] => Sprint Planning [question_id] => 4 ) [4] => stdClass Object ( [id] => 1 [question] => Each user story in the sprint is Sprint Ready [question_category] => 1 [created] => [category_name] => Sprint Planning [question_id] => 5 ) [5] => stdClass Object ( [id] => 1 [question] => There are between 5-20 user stories in the sprint [question_category] => 1 [created] => [category_name] => Sprint Planning [question_id] => 6 ) ) [2] => Array ( ) [3] => Array ( ) ) I'm trying this but get the below error. $get_questions holds the above array. <?php foreach($get_questions as $question){ echo $question->category_name; ?> error: A PHP Error was encountered Severity: Notice Message: Trying to get property of non-object Edited December 25, 2012 by Solarpitch Quote Link to comment https://forums.phpfreaks.com/topic/272358-problem-trying-to-look-through-array/ Share on other sites More sharing options...
Christian F. Posted December 25, 2012 Share Posted December 25, 2012 If we add some proper indenting to that result, it'll be easier to see what's wrong: Array ( [1] => Array ( [0] => stdClass Object ( [id] => 1 [question] => Sprint Planning duration: between 2-4 hours [question_category] => 1 [created] => [category_name] => Sprint Planning [question_id] => 1 ) [1] => stdClass Object ( [id] => 1 [question] => Team used Roman Voting to agree what user stories will be part of the sprint [question_category] => 1 [created] => [category_name] => Sprint Planning [question_id] => 2 ) [2] => stdClass Object ( [id] => 1 [question] => Backlog is correctly prioritized [question_category] => 1 [created] => [category_name] => Sprint Planning [question_id] => 3 ) [3] => stdClass Object ( [id] => 1 [question] => Team discuss user story and acceptance criteria for each story and have clear understanding of what is expected [question_category] => 1 [created] => [category_name] => Sprint Planning [question_id] => 4 ) [4] => stdClass Object ( [id] => 1 [question] => Each user story in the sprint is Sprint Ready [question_category] => 1 [created] => [category_name] => Sprint Planning [question_id] => 5 ) [5] => stdClass Object ( [id] => 1 [question] => There are between 5-20 user stories in the sprint [question_category] => 1 [created] => [category_name] => Sprint Planning [question_id] => 6 ) ) [2] => Array () [3] => Array () ) Hopefully that helps you in spotting the problem? Quote Link to comment https://forums.phpfreaks.com/topic/272358-problem-trying-to-look-through-array/#findComment-1401246 Share on other sites More sharing options...
Solarpitch Posted December 25, 2012 Author Share Posted December 25, 2012 (edited) Apologies, arrays are not my strong point.. indenting it would be a task in itself. And I still havent a clue what the problem is. EDIT: I'm thinking I might need to go down the route of something like but not really sure. <?php foreach($get_questions as $key => $value){ ?> .. Edited December 25, 2012 by Solarpitch Quote Link to comment https://forums.phpfreaks.com/topic/272358-problem-trying-to-look-through-array/#findComment-1401247 Share on other sites More sharing options...
Barand Posted December 25, 2012 Share Posted December 25, 2012 It isn't an array of objects, it's an array of arrays of objects Quote Link to comment https://forums.phpfreaks.com/topic/272358-problem-trying-to-look-through-array/#findComment-1401249 Share on other sites More sharing options...
Solarpitch Posted December 25, 2012 Author Share Posted December 25, 2012 Yeah, while I can mange looking through arrays or objects... I'm confused as to how I can loop through what I just created. Quote Link to comment https://forums.phpfreaks.com/topic/272358-problem-trying-to-look-through-array/#findComment-1401250 Share on other sites More sharing options...
Barand Posted December 25, 2012 Share Posted December 25, 2012 There is an extra dimension in your array. Look at the reformatted version from Christian F <?php foreach($get_questions as $array) { foreach($array as $question) { echo $question->category_name; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/272358-problem-trying-to-look-through-array/#findComment-1401252 Share on other sites More sharing options...
us2rn4m2 Posted December 25, 2012 Share Posted December 25, 2012 Hi, One by one echo $array[1][0]->category_name; All for($i = 0; $i < count($array[1]); $i++) { echo $array[1][$i]->category_name; } Quote Link to comment https://forums.phpfreaks.com/topic/272358-problem-trying-to-look-through-array/#findComment-1401253 Share on other sites More sharing options...
Solarpitch Posted December 25, 2012 Author Share Posted December 25, 2012 Guys, many thanks for your help! I totally understand now cheers! Quote Link to comment https://forums.phpfreaks.com/topic/272358-problem-trying-to-look-through-array/#findComment-1401254 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.