0

Hello There I have the following code:

stdClass Object ( 
  [num_rows] => 3 [row] => Array ( 
     [product_id] => 2439 [author_id] => 39 [author_attribute_id] => 0 
   ) [rows] => Array ( 
     [0] => Array ( 
       [product_id] => 2439 [author_id] =>  39 [author_attribute_id] => 0 ) 
     [1] => Array ( 
       [product_id] => 2439 [author_id] => 156 [author_attribute_id] => 0 ) 
     [2] => Array ( 
       [product_id] => 2439 [author_id] => 684 [author_attribute_id] => 0 )
    ) 
 ) 

now I'd like to extract the author_id values. How do I do this?

6
  • 2
    OK. Have you tried anything at all? Have you looked at the manual for PHP classes/arrays? Commented Sep 22, 2014 at 10:29
  • why no attempt at all? Commented Sep 22, 2014 at 10:29
  • When you say you want to "parse" it, do you mean you have that as a string of text, and want to turn it back into an object? Or just that you have an object (which looks like that when displayed with var_dump or print_r), and don't know how objects work in PHP? Commented Sep 22, 2014 at 10:35
  • @IMSoP I think he's got an object and doesn't know how to access properties Commented Sep 22, 2014 at 10:39
  • @AdamSinclair That would be my guess too, but I was hoping the OP would clarify for themselves. Commented Sep 22, 2014 at 15:52

4 Answers 4

1

For your example: $obj->$row->author_id

Sign up to request clarification or add additional context in comments.

1 Comment

that was helpful but i wanted to get all the author ids from this obj well thanks for the help.......i already sorted the problem
1

You might want to access properties do it like that:

$obj->property

Or if you don't often use object you might find this function useful:

function objectToArray($obj)
{
    if (is_object($obj))
        $obj = get_object_vars($obj);
    if (is_array($obj))
        return array_map(__CLASS__.'::'.__FUNCTION__, $obj);
    else
        return $obj;
}

It converts an Object to an Array.

Comments

0

first of all thanks for all your answers Solved my problem with the following code

 for($i=0;$i<obj->num_rows;$i++)    {
    $array = obj->rows[i];
    echo $array['author_id'];
    }

Well thanks again for posting your answers......and sorry for late reply......

Comments

-1

$object->row->author_id

foreach ($object->rows as $row) {
    echo $row->author_id;
}

2 Comments

Thanks for one of your first posts. You might want to look at syntax highlighting and add some comment to your code.
Welcome to stack overflow Aleksandr. Please add more explanation to your code snippet. This is not self explanatory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.