TechnoDiver Posted September 11, 2021 Share Posted September 11, 2021 Can someone give me a hand looping through this. I've been trying to make an array out of only the 'category fields for hours. Figured I'd ask for help before doing something I regret to my computer -> Quote DB Object ( [_pdo:DB:private] => PDO Object ( ) [_query:DB:private] => PDOStatement Object ( [queryString] => SELECT * FROM categories WHERE type = ? ) [_error:DB:private] => [_results:DB:private] => Array ( [0] => stdClass Object ( [id] => 1 [category] => public health [type] => top ) [1] => stdClass Object ( [id] => 2 [category] => environment [type] => top ) [2] => stdClass Object ( [id] => 3 [category] => global unrest [type] => top ) [3] => stdClass Object ( [id] => 4 [category] => military [type] => top ) [4] => stdClass Object ( [id] => 6 [category] => super powers [type] => top ) [5] => stdClass Object ( [id] => 7 [category] => technology [type] => top ) [6] => stdClass Object ( [id] => 8 [category] => human rights [type] => top ) [7] => stdClass Object ( [id] => 60 [category] => space race [type] => top ) [8] => stdClass Object ( [id] => 67 [category] => globalism [type] => top ) [9] => stdClass Object ( [id] => 87 [category] => government [type] => top ) ) [_count:DB:private] => 10 ) I'm trying to make an array of all the values in each 'category' key Quote Link to comment https://forums.phpfreaks.com/topic/313722-looping-through-an-stdclass-object/ Share on other sites More sharing options...
Barand Posted September 11, 2021 Share Posted September 11, 2021 Post a json_encoded() version. Quote Link to comment https://forums.phpfreaks.com/topic/313722-looping-through-an-stdclass-object/#findComment-1589826 Share on other sites More sharing options...
maxxd Posted September 11, 2021 Share Posted September 11, 2021 If you're looking for the categories, select the category column and save yourself the trouble. $qry = " SELECT category FROM categories; "; $cats = $conn->query($qry, PDO::FETCH_ASSOC); Selecting '*' is inefficient in general from a SQL standpoint, and you're bound to keep running into the complexities you're seeing now. Select what you need in the mode you need it. Quote Link to comment https://forums.phpfreaks.com/topic/313722-looping-through-an-stdclass-object/#findComment-1589831 Share on other sites More sharing options...
TechnoDiver Posted September 11, 2021 Author Share Posted September 11, 2021 Thanks both for the replies. It was actually pretty simple once I thought it through, it was an early morning question and my mind wasn't totally clear -> <?php public function all() { $items = array(); foreach($this->_results as $item) { $items [] = $item->category; //still need to figure this into a variable } return $items; } but I do have one issue here. Might sound really simplistic to some, but has been giving me problems. This is not meant to be a method specific to categories and I kind quite work out how to abstract $item->category to use a parameter value. for example -> <?php public function all($column) { $items = array(); foreach($this->_results as $item) { $items [] = $item->$column; //still need to figure this into a variable } return $items; } doesn't work if $column = "categories" ( as a string). How do I pass the column name in as a parameter?? Quote Link to comment https://forums.phpfreaks.com/topic/313722-looping-through-an-stdclass-object/#findComment-1589833 Share on other sites More sharing options...
Barand Posted September 11, 2021 Share Posted September 11, 2021 Stilll waiting for you to post the data in a processable form as requested. Quote Link to comment https://forums.phpfreaks.com/topic/313722-looping-through-an-stdclass-object/#findComment-1589834 Share on other sites More sharing options...
TechnoDiver Posted September 11, 2021 Author Share Posted September 11, 2021 5 minutes ago, Barand said: Stilll waiting for you to post the data in a processable form as requested. everything in it's time brother, I appreciate and need the help but I can't help when I get the notifications. Here's the json Quote [{"id":"1","category":"public health","type":"top"},{"id":"2","category":"environment","type":"top"},{"id":"3","category":"global unrest","type":"top"},{"id":"4","category":"military","type":"top"},{"id":"6","category":"super powers","type":"top"},{"id":"7","category":"technology","type":"top"},{"id":"8","category":"human rights","type":"top"},{"id":"60","category":"space race","type":"top"},{"id":"67","category":"globalism","type":"top"},{"id":"87","category":"government","type":"top"}] Quote Link to comment https://forums.phpfreaks.com/topic/313722-looping-through-an-stdclass-object/#findComment-1589836 Share on other sites More sharing options...
Barand Posted September 11, 2021 Share Posted September 11, 2021 $json = '[{"id":"1","category":"public health","type":"top"},{"id":"2","category":"environment","type":"top"},{"id":"3","category":"global unrest","type":"top"},{"id":"4","category":"military","type":"top"},{"id":"6","category":"super powers","type":"top"},{"id":"7","category":"technology","type":"top"},{"id":"8","category":"human rights","type":"top"},{"id":"60","category":"space race","type":"top"},{"id":"67","category":"globalism","type":"top"},{"id":"87","category":"government","type":"top"}]'; $array = json_decode($json, 1); // decode as an array $column = 'category'; $categories = array_column($array, $column); // get the $column values echo '<pre>' . print_r($categories, 1) . '</pre>'; gives Array ( [0] => public health [1] => environment [2] => global unrest [3] => military [4] => super powers [5] => technology [6] => human rights [7] => space race [8] => globalism [9] => government ) 1 1 Quote Link to comment https://forums.phpfreaks.com/topic/313722-looping-through-an-stdclass-object/#findComment-1589837 Share on other sites More sharing options...
Barand Posted September 11, 2021 Share Posted September 11, 2021 PS If you want the ID to be the key in the categories array, use $categories = array_column($array, $column, 'id'); // get the category values with id as the key 1 Quote Link to comment https://forums.phpfreaks.com/topic/313722-looping-through-an-stdclass-object/#findComment-1589840 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.