1

I have a web page written in php where i get some info about the user from his linked in profile, thanks to linked in API. It gives me the following json object. with :

$user = json_decode($response);

method i decode the following json object into $user object.

{ "firstName": "Tolga", "lastName": "Evcimen", "skills": { "_total": 2, "values": [ { "id": 1, "skill": {"name": "Microsoft Office"} }, { "id": 2, "skill": {"name": "Microsoft Excel"} } ] } }

what i can't is to work with these values, my php knowledge is a little low, that's why i don't know how to read skills or anything else. The only thing I could read so far is :

$user->firstName , $user->lastName

but I can't get the rest with same approach :(

$user->skills->values[1]->skill->name, or $user->skills->_total

please give me some information about these things

6
  • Do you get the same using $user['skills'] instead of $user->skills? When using {}, you're not creating actual PHP objects, but associative arrays. I would use $user['skills']['values'][1]['skill']['name'] Commented Jun 10, 2013 at 19:05
  • var_dump($user) will let you see the actual structure of what you get by json_decode. Then it will be trivial. Commented Jun 10, 2013 at 19:07
  • Once you have it in $user do a print_r or var_dump to see the structure Commented Jun 10, 2013 at 19:07
  • 2
    Works fine for me with the JSON you posted: codepad.org/RvYCLvNI. We cannot really help you if the code and everything else you post actually works. Commented Jun 10, 2013 at 19:08
  • 2
    @Alejandro: By default, JSON encoded objects are converted to objects of class stdClass, not arrays. php.net/manual/en/function.json-decode.php Commented Jun 10, 2013 at 19:09

3 Answers 3

2

This works:

echo $user->firstName;
echo $user->skills->values[0]->id;
echo $user->skills->values[0]->skill->name;

Results:

Tolga
1
Microsoft Office

Test it on PHP fiddle: http://phpfiddle.org/main/code/x18-z6f

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

Comments

1

I had no problem with this, maybe post a code example so we can see where you went wrong.

$user->skills->values[1]->skill->name

Example:

<?php
    $response = '{
      "firstName": "Tolga",
      "lastName": "Evcimen",
      "skills": {
        "_total": 2,
        "values": [
          {
            "id": 1,
            "skill": {
              "name": "Microsoft Office"
            }
          },
          {
            "id": 2,
            "skill": {
              "name": "Microsoft Excel"
            }
          }
        ]
      }
    }';

    $user = json_decode($response);

    var_dump($user->skills->values[1]);

?>

Output:

object(stdClass)#5 (2) {
  ["id"]=>
  int(2)
  ["skill"]=>
  object(stdClass)#6 (1) {
    ["name"]=>
    string(15) "Microsoft Excel"
  }
}

Comments

0

thanks a lot, it turned out I made some other mistakes in the code, instead of echo I used print, and kinda mixed it up in a bad way. thats why I couldn't achive the right behaviour in code.

$user = fetch('GET', '/v1/people/~:(firstName,lastName,skills)');
print "\n <\br> Hello $user->firstName $user->lastName."; 
print " $user->skills->values[0]->skill->name";
echo $user->skills->values[0]->skill->name;
exit; 

doesn't work :)

echo "\n <\br> Hello $user->firstName $user->lastName.";  
echo $user->skills->values[0]->skill->name;

works fine.

Grateful for your concerns.

Comments

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.