Jump to content

Looping through an stdClass Object


TechnoDiver
 Share

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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??                                                                                                 

Link to comment
Share on other sites

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"}]

 

Link to comment
Share on other sites

$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
)

 

  • Like 1
  • Great Answer 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.