I have some XML that is being returned to be as an object, like this:
SwitchvoxResponse Object
(
[apiStatus:private] => success
[apiErrors:private] => Array
(
)
[apiResult:private] => Array
(
[calls] => Array
(
[page_number] => 1
[total_pages] => 1
[items_per_page] => 50
[total_items] => 1
[call] => Array
(
[0] => Array
(
[id] => 14301
[origination] => outgoing
[start_time] => 2011-06-17 13:40:58
[from] => CALLER_NAME <4485>
[from_account_id] => 1120
[from_name] => CALLER_NAME
[from_number] => 4485
[to] => CALLEE_NAME <6534>
[to_account_id] => 1101
[to_name] => CALLEE_NAME
[to_number] => 6534
[total_duration] => 47
[talk_duration] => 43
[events] => Array
(
[event] => Array
(
[0] => Array
(
[start_time] => 2011-06-17 13:40:58
[type] => OUTGOING
[display] => Dialed number (6534)
)
[1] => Array
(
[start_time] => 2011-06-17 13:40:58
[type] => INTERNAL
[display] => Rang CALLEE_NAME <6534>
)
[2] => Array
(
[start_time] => 2011-06-17 13:41:02
[type] => TALKING
[display] => Talked to CALLEE_NAME <6534> for 43 seconds
)
[3] => Array
(
[start_time] => 2011-06-17 13:41:45
[type] => HANGUP
[display] => Call was hung up by CALLER_NAME <4485>
)
)
)
)
)
)
)
)
How do I pull out the values of these variables?
The results can be accessed via the SwitchvoxResponse::getResult() method. Given $object is the SwitchvoxResponse object quoted in the question, the example below loops over each call and prints the from values.
$result = $object->getResult();
foreach ($result['calls']['call'] as $call) {
echo $call['from'];
}
Similarly, the response status is fetched via $object->getResponseStatus() and any errors via $object->getErrors().
The response statuses can be one of SV_RESPONSE_SUCCESS, SV_RESPONSE_FAULT or SV_RESPONSE_FAILED.
Edit re. comments
To get the items for the first call only, simply do:
$result = $object->getResult();
$call = $result['calls']['call'][0];
// And access the values like
echo $call['from_name'];
All three properties are marked private, so they would have to be obtained through accessor methods marked public
Related
I am getting below json data thru Shiprocket API. Now I want to extract value of below variables in PHP code from this json.
I have tried to use json_decode but it did not work and show null value:
$data = json_decode($json);
$sr_status = $data['shipment_status'];
Please suggest the code to retrieve below variables value.
shipment_status , awb_code , courier_company_id
Array
(
[0] => stdClass Object
(
[tracking_data] => stdClass Object
(
[track_status] => 1
[shipment_status] => 7
[shipment_track] => Array
(
[0] => stdClass Object
(
[id] => 180339484
[awb_code] => 11150911492
[courier_company_id] => 55
[shipment_id] => 1711169662
[order_id] => 233223781187
[pickup_date] => 2023-01-11 03:02:00
[delivered_date] => 2023-01-16 12:22:00
[weight] => 0.25
[packages] => 1
[current_status] => Delivered
[delivered_to] => Solapur
[destination] => Solapur
[consignee_name] => ABC
[origin] => Ludhiana
[courier_agent_details] =>
[edd] =>
)
)
[shipment_track_activities] => Array
(
[0] => stdClass Object
(
[date] => 2023-01-16 12:22:00
[status] => 000-T-DL
[activity] => SHIPMENT DELIVERED
[location] => SOLAPUR
[sr-status] => 7
[sr-status-label] => DELIVERED
)
[1] => stdClass Object
(
[date] => 2023-01-16 11:34:00
[status] => 002-S-UD
[activity] => SHIPMENT OUTSCAN
[location] => SOLAPUR
[sr-status] => 17
[sr-status-label] => OUT FOR DELIVERY
)
)
[track_url] => https://shiprocket.co//tracking/11150911492
[etd] => 2023-01-14 17:02:00
[qc_response] => stdClass Object
(
[qc_image] =>
[qc_failed_reason] =>
)
)
)
)
you can try this:
$array = ...; // Your array here
$data= json_decode($array);
$shipment_status = $data[0]->tracking_data->shipment_status;
$awb_code = $data[0]->tracking_data->shipment_track[0]->awb_code;
$courier_company_id = $data[0]->tracking_data->shipment_track[0]->courier_company_id;
Or use $data = json_decode($json,true); which return an array where you can use
foreach($data as $val) {
$shipment_status = $val['tracking_data']['shipment_status'];
foreach ($val['shipment_track'] as $value) {
$awb_code = $value['awb_code'];
$courier_company_id = $value['courier_company_id'];
}
}
I am trying to display the data from Bigcommerce through php when I run the Bigcommerce::getCategories() i getting a array of data like this:
[0] => Bigcommerce\Api\Resources\Category Object
(
[ignoreOnCreate:protected] => Array
(
[0] => id
[1] => parent_category_list
)
[ignoreOnUpdate:protected] => Array
(
[0] => id
[1] => parent_category_list
)
[fields:protected] => stdClass Object
(
[id] => 88
[parent_id] => 0
[name] => Dell
[description] =>
[sort_order] => 0
[page_title] =>
[meta_keywords] =>
[meta_description] =>
[layout_file] =>
[parent_category_list] => Array
(
[0] => 88
)
[image_file] =>
[is_visible] => 1
[search_keywords] =>
[url] => /dell/
)
[id:protected] => 88
[ignoreIfZero:protected] => Array
(
)
[fieldMap:protected] => Array
(
)
)
but when I try to pass this to JQuery so that I can display it using this statement: <?php echo json_encode($categories); ?> I am getting an array of empty objects, what is the right way to get the array of objects in Bigcommerce API? Thanks.
From the first line of your code:
[0] => Bigcommerce\Api\Resources\Category Object
You have an object, not an array. Try casting it to an array first:
$array = (array) $yourObject;
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);
First off, I'm new to PHP and coding in general, so this might be quite an obvious answer.
I'm currently working with the Strava API, and I'm trying to extract data from an Array/Object which is the result of the following API call:
$recentactivities = $api->get('athlete/activities', array('per_page' => 100));
which returns:
Array (
[1] => stdClass Object (
[id] => XXXX
[resource_state] => 2
[external_id] => XXXX
[upload_id] => XXXX
[athlete] => stdClass Object (
[id] => XXXX
[resource_state] => 1
)
[name] => Let\'s see if I can remember how to do this cycling malarkey...
[distance] => 11858.3
[moving_time] => 1812
[elapsed_time] => 2220
[total_elevation_gain] => 44
[type] => Ride
[start_date] => 2014-07-12T13:48:17Z
[start_date_local] => 2014-07-12T14:48:17Z
[timezone] => (
GMT+00:00
) Europe/London
[start_latlng] => Array (
[0] => XXXX
[1] => XXXX
)
[end_latlng] => Array (
[0] => XXXX
[1] => -XXXX
)
[location_city] => XXXX
[location_state] => England
[location_country] => United Kingdom
[start_latitude] => XXXX
[start_longitude] => XXXXX
[achievement_count] => 4
[kudos_count] => 1
[comment_count] => 0
[athlete_count] => 1
[photo_count] => 0
[map] => stdClass Object (
[id] => a164894160
[summary_polyline] => XXXX
[resource_state] => 2
)
[trainer] =>
[commute] =>
[manual] =>
[private] =>
[flagged] =>
[gear_id] => b739244
[average_speed] => 6.544
[max_speed] => 10.8
[average_cadence] => 55.2
[average_temp] => 29
[average_watts] => 99.3
[kilojoules] => 179.9
[device_watts] =>
[average_heartrate] => 191.2
[max_heartrate] => 200
[truncated] =>
[has_kudoed] =>
)
This repeats for the most recent activities.
I'm attempting to extract average_heartrate, which I can do for a single object using the following:
$recentactivities[1]->average_heartrate;
but I'd like to extract all instances of average_heartrate from the Array. I've tried to use a foreach statement, but to be honest, I have no idea where to start.
Any help would be much appreciated.
It's actually pretty simple you can indeed use a foreach loop:
foreach($myArray as $obj){
//$obj is an object so:
if(isset($obj->average_heartrate)){
echo $obj->average_heartrate;
}
}
With this code you iterate through your array with objects and save the wanted array within an array so you can work further with it.
$heartrateArray = array(); // create a new array
//iterate through it with an foreach
foreach($recentactivities as $activity){
// save the average_heartrate as a value under a new key in the array
$heartrateArray[] = $activity->average_heartrate;
}
I have the following code:
<?php
//The company_array:
$company_array = array(
"AAA" => "AAA",
"BBB" => "BBB",
"CCC" => "CCC",
"DDD" => "DDD"
);
$platform_data = 'PC'; //Just to keep it short :)
foreach ($company_array as $company) {
if ($stmt = $mysqli->prepare("SELECT price, time FROM $company WHERE platform = ? ORDER BY time ASC")) {
$stmt->bind_param("s", $platform_data);
$stmt->execute();
$stmt->bind_result($price[$company], $time[$company]);
$i=0;
while ($stmt->fetch()) {
$company_info[$company][$i] = array('Price' => $price[$company], 'Time' => $time[$company]);
$i++;
}
$stmt->close();
}
?>
Now I had some issues with this, the last iteration of the loop seems to break, if I print $company_info all but the last company displays fine, the last one seem to repeat the last value for all the rows:
Array
(
[AAA] => Array
(
[0] => Array
(
[Price] => 626.8600
[Time] => 2013-09-27 14:30:06
)
[1] => Array
(
[Price] => 615.5900
[Time] => 2013-09-27 15:45:05
)
[2] => Array
(
[Price] => 604.7400
[Time] => 2013-09-27 17:45:05
)
)
[BBB] => Array
(
[0] => Array
(
[Price] => 246.7200
[Time] => 2013-09-27 14:30:06
)
[1] => Array
(
[Price] => 245.4700
[Time] => 2013-09-27 15:45:05
)
[2] => Array
(
[Price] => 244.8300
[Time] => 2013-09-27 17:45:05
)
)
[CCC] => Array
(
[0] => Array
(
[Price] => 189.0900
[Time] => 2013-09-27 14:30:06
)
[1] => Array
(
[Price] => 188.9800
[Time] => 2013-09-27 15:45:05
)
[2] => Array
(
[Price] => 188.8900
[Time] => 2013-09-27 17:45:05
)
)
[DDD] => Array
(
[0] => Array
(
[Price] => 134.3100
[Time] => 2013-10-06 13:30:06
)
[1] => Array
(
[Price] => 134.3100
[Time] => 2013-10-06 13:30:06
)
[2] => Array
(
[Price] => 134.3100
[Time] => 2013-10-06 13:30:06
)
)
)
As you can see company AAA, BBB and CCC all have different Prices and Times in each of their arrays, but company DDD has the same value repeated 3 times (the last value in the database) when the values should be different in the same way as the other companies.
Now from what I have read I am doing this wrong, instead of using foreach I should use implode() on the array and use that, but its confusing me since I can not find a good example where it is used on the FROM field (seems to only be used on WHERE)
My question would be, how can I get away from using a foreach loop since it seems to be causing issues, and use the proper implode() method on the FROM field?
I am guessing I need to modify my while loop also so that the output remains in the same format.