So in my code I have:
var_dump($poll);
which prints:
object(stdClass)#2 (9) {
["parent"]=> object(stdClass)#3 (3) {
["className"]=> string(5) "Venue"
["__type"]=> string(7) "Pointer"
["objectId"]=> string(10) "HRSCYpe2FK"
}
["no"]=> int(0)
["updatedAt"]=> string(24) "2011-11-06T23:37:17.917Z"
["creator"]=> object(stdClass)#4 (3) {
["className"]=> string(5) "_User"
["__type"]=> string(7) "Pointer"
["objectId"]=> string(10) "96K81tdpM4"
}
["createdAt"]=> string(24) "2011-11-06T23:37:14.591Z"
["yes"]=> int(1)
["objectId"]=> string(10) "U8ly32582W"
["question"]=> string(20) "Negozio conveniente?"
["reports"]=> object(stdClass)#5 (0) { }
}
and then I want to do:
var_dump($poll["parent"]["objectId"]);
but then it gives me a 500 internal server error. Any idea why this is?
UPDATE:
Pulling from the error log it says:
PHP Fatal error: Cannot use object of type stdClass as array
Looks like your $poll variable is a stdclass in which case, you would access the properties like so
var_dump($poll->parent->objectId)
As for your 500 status, at a guess I'd say PHP is configured to trigger this for errors.
It's because you're tying to do a var_dump on an object and not an array.
$poll->parent->objectId;
object :: object :: property
Related
i am trying to get data from:
http://api.convoytrucking.net/api.php?api_key=public&show=player&player_name=Mick_Gibson
but if i want to get player_name variable with this code:
<?
$js = file_get_contents('http://api.convoytrucking.net/api.php?api_key=public&show=player&player_name=Mick_Gibson');
$pjs = json_decode($js);
var_dump($pjs->{'player_name'});
?>
i get error:
Notice: Trying to get property of non-object in **\htdocs\index.php on
line 9 + var_dump() returns: NULL
var_dump($pjs) returns:
array(1) { [0]=> object(stdClass)#52 (15) { ["player_name"]=> string(11) "Mick_Gibson" ["player_id"]=> int(88) ["rank"]=> string(12) "FIRE TURTLEE" ["lastseen"]=> int(1393797692) ["registration_date"]=> string(19) "2012-08-10 17:01:34" ["last_mission_date"]=> string(19) "2014-03-02 21:41:50" ["time_offset"]=> int(1) ["house_id"]=> int(611) ["fines"]=> int(0) ["wanted"]=> int(0) ["police_badge"]=> bool(true) ["vip"]=> bool(false) ["staff"]=> NULL ["stats"]=> object(stdClass)#53 (23) { ["score"]=> int(2941) ["convoy_score"]=> int(818) ["ARTIC"]=> int(515) ["DUMPER"]=> int(565) ["TANKER"]=> int(56) ["CEMENT"]=> int(163) ["TRASH"]=> int(7) ["ARMORED"]=> int(9) ["VAN"]=> int(501) ["TOW"]=> int(502) ["COACH"]=> int(4) ["LIMO"]=> int(97) ["ARRESTS"]=> int(272) ["GTA"]=> int(67) ["BURGLAR"]=> int(122) ["HEIST"]=> int(1) ["PLANE"]=> int(48) ["HELI"]=> int(12) ["FAILED"]=> int(312) ["OVERLOADS"]=> int(160) ["TRUCK_LOADS"]=> int(1275) ["ODOMETER"]=> int(28320798) ["TIME"]=> int(2078450) } ["achievements"]=> array(4) { [0]=> string(20) "Professional Trucker" [1]=> string(13) "Gravel Hauler" [2]=> string(12) "Delivery Boy" [3]=> string(7) "Wrecker" } } }
This is because $pjs is an one-element-array of objects, so first you should access the array element, which is an object and then access its attributes.
echo $pjs[0]->player_name;
Actually dump result that you pasted tells it very clearly.
The response is an array.
var_dump($pjs[0]->{'player_name'});
#Balamanigandan your Original Post :- PHP Notice: Trying to get property of non-object error
Your are trying to access the Null Object. From AngularJS your are not passing any Objects instead you are passing the $_GET element. Try by using $_GET['uid'] instead of $objData->token
I just need to parse a JSON :
object(stdClass)#363 (3)
{
["type"]=> string(8) "champion"
["version"]=> string(6) "5.22.3"
["data"]=> object(stdClass)#362 (127) {
["Thresh"]=> object(stdClass)#366 (4) { ["id"]=> int(412) ["key"]=> string(6) "Thresh" ["name"]=> string(6) "Thresh" ["title"]=> string(18) "Garde aux chaînes" }
["Aatrox"]=> object(stdClass)#365 (4) { ["id"]=> int(266) ["key"]=> string(6) "Aatrox" ["name"]=> string(6) "Aatrox" ["title"]=> string(17) "Épée des Darkin" }
["Tryndamere"]=> object(stdClass)#368 (4) { ["id"]=> int(23) ["key"]=> string(10) "Tryndamere" ["name"]=> string(10) "Tryndamere" ["title"]=> string(11) "Roi barbare" } etc...
How to parse all the datas from this JSON with PHP by using object return.
Thanks in advance for help.
Use json_decode.
Returns as object:
json_decode($json_string);
Returns as associative array:
json_decode($json_string, true);
From what I guess you used json_decode and it returned object. You can now access object values using variable name let's say $var
echo $var->type; // will output champion
You can also convert json to array by providing second argument set to true
then you can access this data via
echo $var['type'];
I am trying to pull one single piece of data from a json encoded string. I managed to get the string into php's jason format, but I dont understand how to display the exact data I want (confirmations) Every time I try to access any of the data it just gives me a bracket({) and no data. I know I'm doing something wrong with retreiving the data from the variable, but I can't pu tmy finger on it.
Here's the data I want to parse, could someone please show me how to access the confirmations value from within the array of data? Thanks.
( https://projectbuilder.info/link.php?ID=jsontest&CMD=page ) [easier to read]
Source Code:
<?PHP
$return = file_get_contents("https://api.chain.com/v2/bitcoin/transactions/76e6f17cb940745255e2b8439eea5dae945a b148f1fbba98a9fb99c9a5801320?api-key-id=ae7317a1cd4ff0d12e49a77bfd8b9dec");
var_dump(json_decode($return));
echo $return[0]["confirmations"]; //one of my many attempts to get the info, also tried without the leading zero, and different numbers. I understand arrays, I just dont understand how the json data is formatted inside the array.
?>
Output Copy(what do I do with $return to get it's confirmation value of 2896?):
object(stdClass)#1 (11) {
["hash"]=>
string(64) "76e6f17cb940745255e2b8439eea5dae945ab148f1fbba98a9fb99c9a5801320"
["block_hash"]=>
string(64) "00000000000000001646d024d4622a0e4a5c06299d7d776de041bc9c317be1f8"
["block_height"]=>
int(329486)
["block_time"]=>
string(20) "2014-11-11T02:10:21Z"
["chain_received_at"]=>
string(24) "2014-11-11T02:05:48.259Z"
["confirmations"]=>
int(2896)
["lock_time"]=>
int(0)
["inputs"]=>
array(2) {
[0]=>
object(stdClass)#2 (7) {
["transaction_hash"]=>
string(64) "76e6f17cb940745255e2b8439eea5dae945ab148f1fbba98a9fb99c9a5801320"
["output_hash"]=>
string(64) "4f7b6066396e422f1cabd60767093ec6fb4480b60f206c408ad50895541bc023"
["output_index"]=>
int(0)
["value"]=>
int(144475)
["addresses"]=>
array(1) {
[0]=>
string(34) "1HPvAS96JXYUuLDs5CKNh61SvH6NJT1ykH"
}
["script_signature"]=>
string(213) "304602210087289ed01fd7d04e3c7eb5c38ea1944cbc3789658a1122610079a4f0421e2426022100fca6f5f4623bbac131f06ebdc7389ea0c76b4355da9508ecc7b02107385ee79d01 029a79a3cf6f8b90b7c1210e593a21e46c81ffbbe544eb2ab3ebbd89f33e4f8b2e"
["sequence"]=>
int(4294967295)
}
[1]=>
object(stdClass)#3 (7) {
["transaction_hash"]=>
string(64) "76e6f17cb940745255e2b8439eea5dae945ab148f1fbba98a9fb99c9a5801320"
["output_hash"]=>
string(64) "22de0dbb8d72fac9a6e8775f6f80fa3fc991c41d33fcd5081acb49c0479f2a62"
["output_index"]=>
int(1)
["value"]=>
int(7390022)
["addresses"]=>
array(1) {
[0]=>
string(34) "158kR5o6EWWhFEZfLqGDvkgfoyi2Ep2fhA"
}
["script_signature"]=>
string(209) "304402205e2f36c9c22e02767e3accc8bc609b74d5a23c58e4a8edbac24cb4baf1e3feaa022043ad33becd57c5dee03fd1c1d8be1524281f6f49ba1e44a1a2b340f89554c42d01 031181694e14973f71d45f5f3ab73ee0f30dfa3f488bf53c44e46cf9f4f3d3d722"
["sequence"]=>
int(4294967295)
}
}
["outputs"]=>
array(2) {
[0]=>
object(stdClass)#4 (9) {
["transaction_hash"]=>
string(64) "76e6f17cb940745255e2b8439eea5dae945ab148f1fbba98a9fb99c9a5801320"
["output_index"]=>
int(0)
["value"]=>
int(144475)
["addresses"]=>
array(1) {
[0]=>
string(34) "1P5rwnk3GYbxgpxN9M9EziLrpvoih4c8JC"
}
["script"]=>
string(85) "OP_DUP OP_HASH160 f23e1f6dd21bab989f18c14f26bf37b4e2372eef OP_EQUALVERIFY OP_CHECKSIG"
["script_hex"]=>
string(50) "76a914f23e1f6dd21bab989f18c14f26bf37b4e2372eef88ac"
["script_type"]=>
string(10) "pubkeyhash"
["required_signatures"]=>
int(1)
["spent"]=>
bool(false)
}
[1]=>
object(stdClass)#5 (9) {
["transaction_hash"]=>
string(64) "76e6f17cb940745255e2b8439eea5dae945ab148f1fbba98a9fb99c9a5801320"
["output_index"]=>
int(1)
["value"]=>
int(7380022)
["addresses"]=>
array(1) {
[0]=>
string(34) "1DpY5Mu2qTNkwsnPwgqFnaZ1Kq7GpKDboy"
}
["script"]=>
string(85) "OP_DUP OP_HASH160 8c9efff6e8500a36c16a939054a333d81ef23166 OP_EQUALVERIFY OP_CHECKSIG"
["script_hex"]=>
string(50) "76a9148c9efff6e8500a36c16a939054a333d81ef2316688ac"
["script_type"]=>
string(10) "pubkeyhash"
["required_signatures"]=>
int(1)
["spent"]=>
bool(true)
}
}
["fees"]=>
int(10000)
["amount"]=>
int(7524497)
}
{
Have a look at the docs: http://php.net/json_decode
Convert the json-string to a php-array and save it in a variable. Then use it like any other array to get values from it. Like this:
$return = file_get_contents('http://etc.com/');
$decoded_return = json_decode($return);
echo $decoded_return['confirmations']; //Make sure the key exists, obviously
So keep in mind that $return is a string. The json_decode function returns an array, so you have to save that array in a (new - preferably) variable to get values from it.
Hello awesome programmers!
I am so sorry for the the novice question, however, I am having trouble finding a solution to my problem. I am attempting to run a for each loop through my array passed from my controller, however, the data being outputted is not the same as when I run a var_dump($array). I am thinking perhaps that I need to iterate through this object maybe? However, when I attempt to do so, I get a non-object error.
Controller:
$data['user_details'] = $this->ion_auth->user()->row();
View:
var_dump($user_details);
foreach($user_details as $item){
echo $item['email'];
}
The output of this is : "21nfn4111NR12" but should be roger#peterson.net!
I have also tried the object form:
var_dump($user_details);
foreach($user_details as $item){
echo $item->email;
}
However, it results in error trying to get property of non-object!
When I run the var dump I get the following:
object(stdClass)#21 (17) {
["id"]=>
string(1) "2"
["ip_address"]=>
string(14) "119.132.127.01"
["username"]=>
string(12) "roger petereson"
["password"]=>
string(40) "fdZxF/RQo4nZKmbA5XQlwefbc8f8e5c74899c3d0"
["salt"]=>
NULL
["email"]=>
string(19) "roger#peterson.net"
["activation_code"]=>
NULL
["forgotten_password_code"]=>
NULL
["forgotten_password_time"]=>
NULL
["remember_code"]=>
string(22) "44hjOlloLTIrkSrjSBVNie"
["created_on"]=>
string(10) "1404939094"
["last_login"]=>
string(10) "1405099607"
["active"]=>
string(1) "1"
["first_name"]=>
string(6) "Roger"
["last_name"]=>
string(5) "Peterson"
["is_owner"]=>
string(1) "1"
["user_id"]=>
string(1) "2"
}
The following line:
$this->ion_auth->user()->row();
returns an object not an array (check your var_dump output), so you just need to
echo $user_details->email;
i am trying to get data from:
http://api.convoytrucking.net/api.php?api_key=public&show=player&player_name=Mick_Gibson
but if i want to get player_name variable with this code:
<?
$js = file_get_contents('http://api.convoytrucking.net/api.php?api_key=public&show=player&player_name=Mick_Gibson');
$pjs = json_decode($js);
var_dump($pjs->{'player_name'});
?>
i get error:
Notice: Trying to get property of non-object in **\htdocs\index.php on
line 9 + var_dump() returns: NULL
var_dump($pjs) returns:
array(1) { [0]=> object(stdClass)#52 (15) { ["player_name"]=> string(11) "Mick_Gibson" ["player_id"]=> int(88) ["rank"]=> string(12) "FIRE TURTLEE" ["lastseen"]=> int(1393797692) ["registration_date"]=> string(19) "2012-08-10 17:01:34" ["last_mission_date"]=> string(19) "2014-03-02 21:41:50" ["time_offset"]=> int(1) ["house_id"]=> int(611) ["fines"]=> int(0) ["wanted"]=> int(0) ["police_badge"]=> bool(true) ["vip"]=> bool(false) ["staff"]=> NULL ["stats"]=> object(stdClass)#53 (23) { ["score"]=> int(2941) ["convoy_score"]=> int(818) ["ARTIC"]=> int(515) ["DUMPER"]=> int(565) ["TANKER"]=> int(56) ["CEMENT"]=> int(163) ["TRASH"]=> int(7) ["ARMORED"]=> int(9) ["VAN"]=> int(501) ["TOW"]=> int(502) ["COACH"]=> int(4) ["LIMO"]=> int(97) ["ARRESTS"]=> int(272) ["GTA"]=> int(67) ["BURGLAR"]=> int(122) ["HEIST"]=> int(1) ["PLANE"]=> int(48) ["HELI"]=> int(12) ["FAILED"]=> int(312) ["OVERLOADS"]=> int(160) ["TRUCK_LOADS"]=> int(1275) ["ODOMETER"]=> int(28320798) ["TIME"]=> int(2078450) } ["achievements"]=> array(4) { [0]=> string(20) "Professional Trucker" [1]=> string(13) "Gravel Hauler" [2]=> string(12) "Delivery Boy" [3]=> string(7) "Wrecker" } } }
This is because $pjs is an one-element-array of objects, so first you should access the array element, which is an object and then access its attributes.
echo $pjs[0]->player_name;
Actually dump result that you pasted tells it very clearly.
The response is an array.
var_dump($pjs[0]->{'player_name'});
#Balamanigandan your Original Post :- PHP Notice: Trying to get property of non-object error
Your are trying to access the Null Object. From AngularJS your are not passing any Objects instead you are passing the $_GET element. Try by using $_GET['uid'] instead of $objData->token