Getting a specific value from a json response - php

I have this code
$client = json_decode($client);
echo "<pre>";
print_r($client);
which produces there
Array
(
[0] => stdClass Object
(
[id] => 1
[name] => Jojohn#doe.com
[email_verified_at] =>
[password] => $2y$10$pAvJ9/K7ZPOqw10WhfmToumK0TY1XihY8M9uAEEs4GkHZr4LdGc4e
[remember_token] =>
[created_at] => 2020-07-29 21:08:02
[updated_at] =>
[userid] => 2
[account_rep] => 3
)
)
My question is how do I get the value of name and account_rep I tried
echo $client['0']['object']['name'];
but that does not work it just throws an error
Cannot use object of type stdClass as array

json_decode($variable), is used to decode or convert a JSON object to a PHP object.
So you could do this as $client['0'] is an object.
echo $client['0']->name;
But I'd say you should rather convert the json object to associative array instead of PHP object by passing TRUE to as an argument to the json_decode. When TRUE, returned objects get converted into associative arrays.
$client = json_decode($client, true);
Now $client is
Array
(
[0] => Array
(
[id] => 1
[name] => Jojohn#doe.com
[email_verified_at] =>
[password] => $2y$10$pAvJ9/K7ZPOqw10WhfmToumK0TY1XihY8M9uAEEs4GkHZr4LdGc4e
[remember_token] =>
[created_at] => 2020-07-29 21:08:02
[updated_at] =>
[userid] => 2
[account_rep] => 3
)
)
Now you could simply do
echo $client[0]['name'];

Related

How to get value out of this...?

Can someone explain me how to get data out of this...like if I just want subject, description..etc...
stdClass Object
(
[tickets] => Array
(
[0] => stdClass Object
(
[url] => https://codemymobilecom.zendesk.com/api/v2/tickets/1.json
[id] => 1
[external_id] =>
[via] => stdClass Object
(
[channel] => sample_ticket
[source] => stdClass Object
(
[from] => stdClass Object
(
)
[to] => stdClass Object
(
)
[rel] =>
)
)
[created_at] => 2015-04-22T08:30:29Z
[updated_at] => 2015-05-19T06:01:22Z
[type] => incident
[subject] => This is a sample ticket requested and submitted by you
[raw_subject] => This is a sample ticket requested and submitted by you
[description] => This is the first comment. Feel free to delete this sample ticket.
[priority] => high
[status] => closed
[recipient] =>
[requester_id] => 794599791
[submitter_id] => 794599791
[assignee_id] => 794599791
[organization_id] => 39742491
[group_id] => 24344491
[collaborator_ids] => Array
(
)
[forum_topic_id] =>
[problem_id] =>
[has_incidents] =>
[due_at] =>
[tags] => Array
(
[0] => sample
[1] => zendesk
)
[custom_fields] => Array
(
)
[satisfaction_rating] =>
[sharing_agreement_ids] => Array
(
)
[fields] => Array
(
)
[followup_ids] => Array
(
)
[brand_id] => 565681
)
[1] => stdClass Object
(
[url] => https://codemymobilecom.zendesk.com/api/v2/tickets/10.json
[id] => 10 //multiple object like [0]...
Thanks...Any help would be great..
When you need to access to array's key, use []. When you have object, use ->.
echo $obj->tickets[0]->subject; // returns first subject
echo $obj->tickets[0]->description; // returns first description
You can put it into foreach loop, of course to gain all subjects, etc.
It's STD object so use properties
$obj->tickets[0]->subject
$obj->tickets[0]->description
You can obviously loop tickets
foreach($obj->tickets as $ticket)
{
echo $ticket->subject;
echo $ticket->description
}
this is an std object.to get subject and description follow this
$obj->tickets[0]->subject;
$obj->tickets[0]->description;
if you feel better in array just make it array using this code
$array = get_object_vars($obj);

Laravel 5 pagination issue

I am working with laravel 5 pagination and getting some problems.
Here is my code DB::table('users')->paginate(2) and it returns an object of LengthAwarePaginator Like this :
Illuminate\Pagination\LengthAwarePaginator Object
(
[total:protected] => 4
[lastPage:protected] => 2
[items:protected] => Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[id] => 3
[Name] => shivani
[email] => shivani.ruhela#hotmail.com
[password] => $2y$10$iD1by60rSBvrMjuqwmYLseNIrd4jmvK8sXUEYiXfDjJVcBD02jEbK
[updated_at] => 2015-04-27 05:17:34
[created_at] => 2015-04-27 05:17:30
[remember_token] => guVL5RdcmRXedSBMsfSZSlCeFPfRjEq8vSNQNjtED2ytBHaPCZ3N8G3dmj6C
)
[1] => stdClass Object
(
[id] => 4
[Name] => shivani
[email] => shivani2.ruhela#hotmail.com
[password] => $2y$10$iD1by60rSBvrMjuqwmYLseNIrd4jmvK8sXUEYiXfDjJVcBD02jEbK
[updated_at] => 2015-04-27 05:17:34
[created_at] => 2015-04-27 05:17:30
[remember_token] => guVL5RdcmRXedSBMsfSZSlCeFPfRjEq8vSNQNjtED2ytBHaPCZ3N8G3dmj6C
)
)
)
[perPage:protected] => 2
[currentPage:protected] => 1
[path:protected] => http://localhost/shivani/public/check/user-list
[query:protected] => Array
(
)
[fragment:protected] =>
[pageName:protected] => page
)
My problem is that I want to iterate through this object and retrieve items object that holds database data i.e. name , email etc.
But I don't know how to access protected members from the returned object.
$data = DB::table('users')->paginate(2)->toArray()
Try this.. it might work for you
You can just use the returned object like it where an array:
$users = DB::table('users')->paginate(2);
foreach($users as $user){
echo $user->email;
}
You can access paginated objects same way you can access any other collection. For example
foreach(DB::table('users')->paginate(2) as $object) {
echo $object->name;
}

HubSpot api json decode

I am trying to parse some data out of the Hubspot API response. The response looks like this json_decoded:
stdClass Object(
[addedAt] => 1411052909604
[vid] => 24
[canonical-vid] => 24
[merged-vids] => Array
(
)
[portal-id] => XXXXX
[is-contact] => 1
[profile-token] => AO_T-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[profile-url] => https://app.hubspot.com/contacts/XXXXX/lists/public/contact/_AO_T-XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[properties] => stdClass Object
(
[lastname] => stdClass Object
(
[value] => testtt
)
[firstname] => stdClass Object
(
[value] => test
)
[lastmodifieddate] => stdClass Object
(
[value] => 1411052906670
)
)
[form-submissions] => Array
(
[0] => stdClass Object
(
[conversion-id] => 85d24dd2-9ee9-4d47-b8f3-3035acbd8f3b
[timestamp] => 1411052834097
[form-id] => fb16efd9-23cc-4511-889c-204fc8b41dba
[portal-id] => 401824
[page-url] => http://wbl-1.hs-sites.com/test
[canonical-url] => http://wbl-1.hs-sites.com/test
[content-type] => landing-page
[page-title] => test
[page-id] => 1570433242
[title] => Default Form (Sample)
[first-visit-url] => http://wbl-1.hs-sites.com/test
[first-visit-timestamp] => 1411052722970
[meta-data] => Array
(
)
)
)
[list-memberships] => Array
(
)
[identity-profiles] => Array
(
[0] => stdClass Object
(
[vid] => 24
[identities] => Array
(
[0] => stdClass Object
(
[type] => EMAIL
[value] => test#user.com
[timestamp] => 1411052834097
)
[1] => stdClass Object
(
[type] => LEAD_GUID
[value] => 0b6acf21-6cee-4c7b-b664-e65c11ee2d8e
[timestamp] => 1411052834201
)
)
)
)
[merge-audits] => Array
(
)
)
I'm looking specifically to try to dig out an email inside the indentities-profile area.
I've tried to do the following:
echo $results->contacts[0]->identity-profiles;
But it just gives me a value of 0
Then I try to go further into the array by doing:
echo $results->contacts[0]->identity-profiles[0];
But at that point - I get a parse error:
Parse error: syntax error, unexpected '['
What am I doing wrong? And how can I dig all the way down to
identity-profiles[0]->identities->[0]->value
which should equal: test#user.com
What am I missing?
As mentioned in the comment I would suggest to decode the JSON to an associative array by passing true as second parameter to json_decode. Example: json_decode($data, true) Than you could access your identity profiles by:
$results['contacts'][0]['identitiy-profiles']
If you still want to get the results as an object you have to access the properties the following way because they contain a -:
$results->contacts[0]->{'identity-profiles'}

how to manage json data in php

I called JSON data and got following output:
stdClass Object
(
[request] => stdClass Object
(
[Target] => Affiliate_Report
[Format] => json
[Service] => HasOffers
[Version] => 3
[Method] => getConversions
[api_key] => my_key
[NetworkId] => icubes
[fields] => Array
(
[0] => Offer.name
[1] => Browser.display_name
[2] => Stat.payout
[3] => Stat.sale_amount
[4] => Stat.status
[5] => Stat.datetime
[6] => Stat.ip
[7] => Stat.ad_id
[8] => Stat.affiliate_info1
)
)
[response] => stdClass Object
(
[status] => 1
[httpStatus] => 200
[data] => stdClass Object
(
[page] => 1
[current] => 100
[count] => 75
[pageCount] => 1
[data] => Array
(
[0] => stdClass Object
(
[Offer] => stdClass Object
(
[name] => Myntra (CPS)
)
[Browser] => stdClass Object
(
[display_name] => Chrome
)
[Stat] => stdClass Object
(
[payout] => 150.00000
[sale_amount] => 954.00000
[status] => rejected
[datetime] => 2014-03-31 19:49:50
[ip] => 103.226.84.249
[ad_id] => 102ecc5fe230883c195d8a0e84ef7f
[affiliate_info1] =>
)
)
[1] => stdClass Object
(
[Offer] => stdClass Object
(
[name] => Myntra (CPS)
)
[Browser] => stdClass Object
(
[display_name] => Firefox
)
[Stat] => stdClass Object
(
[payout] => 270.00000
[sale_amount] => 545.00000
[status] => approved
[datetime] => 2014-04-18 12:00:20
[ip] => 27.0.51.119
[ad_id] => 10256740541d68b117955aa58529a6
[affiliate_info1] =>
)
)
I want to display that data in tabular form using table tag and also want to insert that data into a MySQL database.
But I dont understand the array format of data.
My code:
$result = file_get_contents($base);
$obj = json_decode($result);
echo"<table>";
foreach($obj as $item) {
echo"
<tr>
<td>$item['Offer.name']</td>
<td>$item['Browser.display_name']</td>
<td>$item['Stat.payout']</td>
<td>$item['Stat.sale_amount']</td>
<td>$item['Stat.datetime']</td>
<td>$item['Stat.ip']</td>
</tr>
";
}
echo"</table>";
This code gives me a blank output.
i used following code and got output but some errors are still there.
$result = file_get_contents($base);
$obj = json_decode($result, true);
echo"<table border=1>";
$i = 0;
foreach($obj['response'] as $item) {
for($i=0;$i<=10;$i++)
{
echo"
<tr>
<td>{$item['data'][$i]['Offer']['name']}</td>
<td>{$item['data'][$i]['Stat']['payout']}</td>
<td>{$item['data'][$i]['Stat']['sale_amount']}</td>
<td>{$item['data'][$i]['Stat']['status']}</td>
<td>{$item['data'][$i]['Stat']['datetime']}</td>
<td>{$item['data'][$i]['Stat']['ip']}</td>
</tr>
";
}
}
echo"</table>";
my output:
why upper rows are displaying blank values when there is no blank data is present?
and what are those errors?
You're using the wrong syntax. It's difficult to see exactly the results you're expecting, the values of the objects being returned look like they are objects of the original json themselves and without any useful data. However, to clarify for you:
You're returning the json_decode() results as an object and trying to access it as an array.
To return an associative array you need to add the 'assoc' parameter to the function, to read:
json_decode($result, true)
and you were trying to eco out the value directly from the associative array, you need to access it via the key, which will give you the value to read:
$result = file_get_contents($base);
$obj = json_decode($result, true);
echo"<table>";
foreach($obj['request'] as $item) {
echo"
<tr>
<td>{$item['fields'][0]}</td>
<td>{$item['fields'][1]}</td>
<td>{$item['fields'][2]}</td>
<td>{$item['fields'][3]}</td>
<td>{$item['fields'][4]}</td>
<td>{$item['fields'][5]}</td>
</tr>
";
}
echo"</table>";
Also, I've added the curly brackets inside the string to escape the variable expressions.
Since you're getting objects in your response, first try json_decode($result->request) and generally don't use [ ] as you normally would as if your result was array
EDIT: Try outputting the response like this:
echo '<pre>';
print_r($your-variable-here);
echo '</pre>';
Then let me know of the output

how to convert Object array to array while single value returning

how to convert Object array to array while single value returning ,
here is the o/p what i am getting while i am getting only one hotel, how can i convert it in a way to access it as a 0th [0] value shown in code. i have problems in accessing when i m getting single hotel in o/p. print_r($result); via foreach()
stdClass Object
(
[cityId] => 000000000020
[checkInDate] => 2013-12-20
[checkOutDate] => 2013-12-21
[customerId] =>
[customerAccountType] => SH01
[customerType] =>
[currency] => INR
[noOfRoomsRequested] => 1
[searchAvailabilityResult] => stdClass Object
(
[hotelId] => IXW1
[hotelName] => Ginger Jamshedpur
)
)
===what i want is ================================
stdClass Object
(
[cityId] => 000000000020
[checkInDate] => 2013-12-20
[checkOutDate] => 2013-12-21
[customerId] =>
[customerAccountType] => SH01
[customerType] =>
[currency] => INR
[noOfRoomsRequested] => 1
[searchAvailabilityResult] => Array
(
[0] => stdClass Object
(
[hotelId] => IXW1
[hotelName] => Ginger Jamshedpur
)
)
)
in case of single value
$result->searchAvailabilityResult = array($result->searchAvailabilityResult);
Many thanks to #Maciej Sz
$result->searchAvailabilityResult = get_object_vars($result->searchAvailabilityResult);

Categories