This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 6 years ago.
I have a JSON data which I am trying to parse, but my code is not working. I get the error
unexpected '->' (T_OBJECT_OPERATOR).
The array structure looks like this:
Array
(
[mautic.lead_post_save_update] => Array
(
[0] => Array
(
[lead] => Array
(
[isPublished] => 1
[dateAdded] => 2016-09-15T08:08:20+00:00
[createdBy] => 1
[createdByUser] => Deepak Tiwari
[dateModified] => 2016-12-20T14:20:36+00:00
[modifiedBy] => 1
[modifiedByUser] => Deepak Tiwari
[id] => 149269
[points] => 100
[color] =>
[fields] => Array
(
[core] => Array
(
[compulsation] => Array
(
[id] => 39
[label] => Job
[alias] => compulsation
[type] => text
[group] => core
[field_order] => 2
[object] => lead
[value] => 0
and so on. I need to access the value of "job compultion". The code I am using is:
$json = file_get_contents('php://input');
$data=json_decode($json,TRUE);
$job=data->mautic.lead_post_save_update->0->lead->fields->core->compulsation->value;
I am not getting any value in $job.
You're using object property syntax (->) to access array elements, which won't work. Your code should use array syntax ([...]), like this:
$json = file_get_contents('php://input');
$data=json_decode($json,TRUE);
$job=data['mautic.lead_post_save_update'][0]['lead']['fields']['core']['compulsation']['value'];
That's what the error message is trying to tell you. A decent IDE, like PHPStorm (no affiliation), would help you catch errors like this.
Your json_decode() is not creating a object. Its an array and you can get your variables by using [] instead of the ->
Related
This question already has answers here:
Pretty-Printing JSON with PHP
(27 answers)
Closed 6 months ago.
Having an array printed with print_r method:
Array
(
[0] => Array
(
[id] => 44
[item_level] => 0
[position] => 10
[parent_position] =>
[title] => PHP Tutorial
[qty] => 0
[comment] =>
[status] =>
[unit] =>
)
[1] => Array
(
[id] => 46
[item_level] => 1
[position] => 20
[parent_position] =>
[title] => Algorithms
[qty] => 1
[comment] =>
[status] =>
[unit] =>
)
[2] => Array
(
[id] => 48
[item_level] => 2
[position] => 30
[parent_position] => 20
[title] => PHP and MySQL Databases
[qty] => 1
[comment] =>
[status] =>
[unit] =>
)
)
I'm trying to encode it as JSON using either:
json_encode($array, JSON_NUMERIC_CHECK);
json_encode($array);
The output in both cases is the following though:
[{"id":"44","item_level":0,"position":10,"parent_position":"","product_id":"","title":"PHP
Tutorial","qty":0,"comment":"","status":"","unit":""},{"id":"46","item_level":1,"position":20,"parent_position":"","title":"Algorithms","qty":1,"comment":"","status":"","unit":""},{"id":"48","item_level":2,"position":30,"parent_position":20,"title":"PHP
and MySQL
Databases","qty":1,"comment":"","status":"","unit":""}]
Yes, there are the newline characters put between some, but not all, space-separated words. In Postman, the response arrives broken into multiple lines, the same happens when my frontend gets this kind of response, causing it to report JSON parsing error.
How to transform the array to JSON format properly?
PHP version here is 7.0.33.
Tried to look for any non-printable characters that might have been added on the way, as it was suggested in the comments, but wasn't able to locate any.
Anyway, pretty printing solved the issue in a satisfying way:
json_encode($array, JSON_PRETTY_PRINT);
The only thing to be considered, is casting numbers to proper types on arrival to frontend, since JSON_PRETTY_PRINT prints out all of them as strings (i.e. {"quantity": "1"}).
This question already has answers here:
How to get single value from this multi-dimensional PHP array [duplicate]
(6 answers)
Closed 5 years ago.
I have an array:
print_r($response);
will show:
Response: Array
(
[errors] => false
[return] => Array
(
[price_recurring] => 0
[country_code] => NL
[invoicemethod] => instant
[product_id] => 0
[affiliate] =>
[number_of_periods] => 1
[description] => Betaling voor eventid 274
[period_duration] => 1 month
[date] => 29/11/2017 19:42
[order_quantity] => 1
[vat] => 21
[amount_total] => 4599
[total_paused_days] => 0
[custom] => WEB1511980957x194237
[emailaddress] => ik#ik.nl
[amount_affiliate_initial] => 0
[total] => 45,99
[current_status] => completed
[amount_affiliate_recurring] => 0
[amount_total_affiliate] => 0
[id] => 1790226
[price_initial] => 4599
[current_number_of_periods] => 1
[firstname] =>
)
)
How can i extract the value 'custom'?
I've tried:
$response = array_shift($response);
echo $response['custom'];
but it will show nothing. What am i doing wrong here? I know i am close.
$response is an array with two keys errors and return. $response['return'] in turn, is an array with several keys, including custom. Therefore, to access custom, you need to reference $response['return']['custom']
you have an array inside an array
$response["return"]["custom"]
also try to var_dump (or print_r depends on what you prefer) instead of echoing when you try to navigate
var_dump( $response["custom"]);
would tell you a lot more than echoing null, which prints nothing
This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 6 years ago.
Find ID value through php
stdClass Object ( [Providers] => Array (
[0] => stdClass Object (
[Main_Fax] =>
[Signed_POA] => No
[Signed_HIPAA] => No
[Website] => www.google.com
[VAR_Provider_Type] => Template Test
[State_Temp] =>
[City] =>
[Notes] =>
[Main_Phone] =>
[ZIP] =>
[Provider_Type_Temp] =>
[State] => Alaska
[Signed_Purchae_Agreement] => No
[Reimbursement_Rate] => 0
[ID] => 977948000009029003
[Provider_Name] => Amit
[Address_Line_1] =>
[In_Network] => No
[Address_Line_2] =>
)
)
)
Seprate this json & show id value through php -> means this value [ID] => 977948000009029003
Let your array is $arr.
Now try this:
$arr->Providers[0]->ID;
Result:
977948000009029003
$data->Providers[0]->ID
See the structure of the object you gave us. You can see there's an object with a item called Providers. You can then see that the value of "Providers" is an array indexed by a integer, which inside it has an Object. The object contains the ID you wish to obtain.
Object > Array > Object
I got the following array as a result of an output from a web service? I printed the values in the array using the print_r() method as stated in the following description.
ARRAY OUTPUT:
Array
( [0] => stdClass Object
( [return] => stdClass Object
( [data] => stdClass Object
(
[status] => 50000
[adminUser] => 1
[atdUserid] => 58
[category] => [client] => [cur_designation] => TL
[currentEmpId] => E058
[digitPrefix] => 8,5,1,3,7,0
[email] => jaliya#codegen.net
[employeeId] => 58
[employee_status] => 1
[firstName] => Jaliya
[lastName] => Seneviratna
[last_login_date] => stdClass Object ( [date] => 6 [month] => 2 [year] => 2015 )
[letterPrefix] => D,C,U,T,Z,E
[loginName] => jaliya
[resourceStatus] =>
[taskPassword] => d6188c72995d80e1a8e00d34987e0f6b
[userId] => 118 )
[reason] => Success
[refetch] => 1
[status] => 1
) ) )
I got the above array by calling a webservice in php. I want to get the details out of this array. And the problem was that I couldn't get the stdClass objects casted into the right type. I tried the following code but it is not working. Can anyone help me to get the values inside the data[] out in php. I used the following code and it is not working and giving an exception.
CODE USED:
print_r(array_values($quote));
echo $quote[0]->data;
The exception was the following...
EXCEPTION RECIEVED:
Notice: Undefined property: stdClass::$data in C:\xampp\htdocs\WebServiceDemo-php\democlient.php on line 27
How to get values out from this array?
Please help me...
convert the object in to array using this function
get_object_vars(array);
USE Can Print each key value of data array like this:-
echo $quote[0]->data->status;
I will Print
50000
This question already has an answer here:
how to decode this JSON string?
(1 answer)
Closed 8 years ago.
Respected All,
I have a string which I got from an api. Now I want to extract useful data from this string. This output is a result of Json query.
the string is
{"PR":{"Url":"http://www.ididthisfilm.com/lex_tmp2/custom-form/","Domain":"http://www.ididthisfilm.com","Title":"Lexicon Of Sustainability – Custom Form","Description":"Welcome to THE LIST EDIT FORM","Pictures":[{"Url":"http://www.ididthisfilm.com/lex_tmp2/wp-content/uploads/2014/05/Lexicon_logo_new_BETA.png","Alt":"Lexicon Of Sustainability","Title":"","SourceType":1,"Width":0,"Height":0,"ParsedAspectRatio":0}],"Videos":[],"AuthorName":null,"ExtraInfo":null},"EM":null}`
How can I get title and description and url in different variables so that I can use them in php.
use built-in function json_decode and you will get the JSON object.
$str=<<<CODE
{"PR":{"Url":"http://www.ididthisfilm.com/lex_tmp2/custom-form/","Domain":"http://www.ididthisfilm.com","Title":"Lexicon Of Sustainability - Custom Form","Description":"Welcome to THE LIST EDIT FORM","Pictures":[{"Url":"http://www.ididthisfilm.com/lex_tmp2/wp-content/uploads/2014/05/Lexicon_logo_new_BETA.png","Alt":"Lexicon Of Sustainability","Title":"","SourceType":1,"Width":0,"Height":0,"ParsedAspectRatio":0}],"Videos":[],"AuthorName":null,"ExtraInfo":null},"EM":null}
CODE;
print_r( json_decode($str) );
which outputs
stdClass Object
(
[PR] => stdClass Object
(
[Url] => http://www.ididthisfilm.com/lex_tmp2/custom-form/
[Domain] => http://www.ididthisfilm.com
[Title] => Lexicon Of Sustainability – Custom Form
[Description] => Welcome to THE LIST EDIT FORM
[Pictures] => Array
(
[0] => stdClass Object
(
[Url] => http://www.ididthisfilm.com/lex_tmp2/wp-content/uploads/2014/05/Lexicon_logo_new_BETA.png
[Alt] => Lexicon Of Sustainability
[Title] =>
[SourceType] => 1
[Width] => 0
[Height] => 0
[ParsedAspectRatio] => 0
)
)
[Videos] => Array
(
)
[AuthorName] =>
[ExtraInfo] =>
)
[EM] =>
)
if any error occurs, try this: json_last_error()