Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

When I dump the variable it has a data:

var_dump($result);

object(stdClass)#2 (5) { ["user_id"]=> string(1) "1" ["username"]=> string(6) "user_name" ["email"]=> string(14) "[email protected]" ["password"]=> string(32) "password" ["test"]=> string(1) "1" }

But when I use foreach no value has return except if I just echo $data; but I want to be specific to the value I want to get.

foreach($result as $data){

echo $data->use_id;

echo $data->username;

}

Why there is no returned value?

share|improve this question
    
use echo $data->user_id not echo $data->use_id; –  Tamil Selvan Mar 3 '14 at 15:15

3 Answers 3

As you wrote:

var_dump($result);
object(stdClass)#2 (5) { ["user_id"]=> string(1) "1" ["username"]=> string(6) "user_name" ["email"]=> string(14) "[email protected]" ["password"]=> string(32) "password" ["test"]=> string(1) "1" }

So the result is an object itself. so if you want specify and fetch exact variable, there's no need to iterate it!

share|improve this answer
    
Sorry, I don't know that yet. By the way thanks. :| –  Jopay Feb 18 '13 at 8:40

You don't need to iterate over the object. Just do this.

echo $result->user_id;
echo $result->username;

With the sample you provided you are treating each property in $result as an object and looking for user_id and user_name (which don't exist).

share|improve this answer
    
only it is actuall username,and not user_name. user_name is value. Look: ["username"]=> string(6) "user_name" –  Bojan Kovacevic Feb 18 '13 at 8:37
    
Thank you. Don't know that yet. :D –  Jopay Feb 18 '13 at 8:39

Try this : json_decode converts json string to array. Ref: http://php.net/manual/en/function.json-decode.php

$array  = json_decode($result, true);

echo "<pre>";
print_r($array);

You can use foreach to $array and echo the result.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.