ProfessionallyInept
ProfessionallyInept

Reputation: 81

How to store int from var_dump/json_decode into a variable in php?

Hello I am connecting bullhorn via the rest api call and I am trying to store an int from var_dump/json_decode into a variable.

The code I am working with is as follows:

$obj = var_dump(json_decode($info, true));
$entityid = $obj->{'entityId'};

var_dump($info);
echo $entityid;

The results I get from the above code is:

array(1) { ["data"]=> array(1) { [0]=> array(5) { ["entityId"]=> int(19) ["entityType"]=> string(9) "Candidate" ["title"]=> string(13) "xxx" ["byLine"]=> string(22) "xxx" ["location"]=> string(10) "xxx" } } }
Notice: Trying to get property of non-object in /Users/.../new_new.php on line 70
string(133) "{"data":[{"entityId":19,"entityType":"Candidate","title":"xxx","byLine":"xxx","location":"xxx"}]}"

I am just trying to get the "entityId" and store it into a variable for further use. I have played around with this code for a while and have seen issues such as, "Undefined property: stdClass::$entityId", "Uncaught Error: Cannot use object of type stdClass as array", "Undefined index: entityId". I have worked through them and am at this point with no clue where to go from here. The top line of the results resembles a database line to me and if this is the case then unfortunately I do not and can not get access to the database. Any input and advice of how to resolve my issue is greatly appreciated.

Upvotes: 1

Views: 158

Answers (2)

Cadu De Castro Alves
Cadu De Castro Alves

Reputation: 647

You logic is incorrect.

Your JSON is returning an array where the first index is data. You can get it via $obj['data'].

Then, you have another array with 0 index: $obj['data'][0]

Finally, you can access the properties, like the entityId: $obj['data'][0]['entityId']

Upvotes: 0

Andreas
Andreas

Reputation: 23958

Don't use var_dump. It's only a debug tool.

Use the array and the keys to get to the item you want.

$arr = json_decode($info, true);
$entityid = $arr['data'][0]['entityId'];

echo $entityid;

Upvotes: 2

Related Questions