i am able to print the response from an api but it is nested array i am not able to save it.
Array
(
[statuscode] => 0
[status] => SUCCESS
[item] => SimpleXMLElement Object
(
[item] => Array
(
[0] => SimpleXMLElement Object
(
[BENEID] => 227287
[BENENAME] => test
[BANKNAME] => test
[BRANCHNAME] => test
[CITY] => test
[STATE] => tetst
[IFSCCODE] => HBNB
[ACCOUNTNO] => 4545345
[MMID] => SimpleXMLElement Object
(
[0] =>
)
[MOBILE] => 0
[BENESTATUS] => Approved
[MMIDSTATUS] => 0
[IFSCSTATUS] => 1
[IMPSMMIDENABLE] => 0
[IMPSIFSCENABLE] => 0
[IMPSNEFTENABLE] => 1
[DELETESTATUS] => 0
)
)
)
)
This is the response i am getting i want to print all fields separately
This very simple just follow the structure of response which you are getting from API
Try this:-
I suppose your repsonse in a array $reponseArray
foreach($reponseArray['item']->item as $value)
{
foreach($value as $key=>$val)
{
echo $key.' = '. $val;
}
}
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 have weird problem with my PHP code. I decoded an elasticsearch json, and I got this array:
Array ( [took] => 1 [timed_out] => [_shards] => Array ( [total] => 6 [successful] => 6 [skipped] => 0 [failed] => 0 ) [hits] => Array ( [total] => 7336 [max_score] => 0.8790647 [hits] => Array ( [0] => Array ( [_index] => logstash-2019.02.25 [_type] => doc [_id] => 5T8GJWkBAZbF3w3t4NF2 [_score] => 0.8790647 [_source] => Array ( [#version] => 1 [log-level] => ERROR [port] => 50906 [host] => 6b14cd1f183d.mynetwork [#timestamp] => 2019-02-25T14:20:01.367Z [Timestamp] => 2019-02-25T13:57:40+0000 [message] => Array ( [0] => 2019-02-25T13:57:40+0000 ERROR something happened in this execution. [1] => something happened in this execution. ) ) ) [1] => Array ( [_index] => logstash-2019.02.25 [_type] => doc [_id] => 7z8GJWkBAZbF3w3t4NF2 [_score] => 0.8790647 [_source] => Array ( [#version] => 1 [log-level] => INFO [port] => 50906 [host] => 6b14cd1f183d.mynetwork [#timestamp] => 2019-02-25T14:20:01.369Z [Timestamp] => 2019-02-25T14:00:13+0000 [message] => Array ( [0] => 2019-02-25T14:00:13+0000 INFO takes the value and converts it to string. [1] => takes the value and converts it to string. ) ) ) ) ) )
What I want is only to print the "messages".
If I run this code:
$echo json_decoded['hits']['hits']['0']['_source']['message']['0']
It echo "2019-02-25T13:57:40+0000 ERROR something happened in this execution."
And if I run this code:
$echo json_decoded['hits']['hits']['1']['_source']['message']['0']
It echo "2019-02-25T14:00:13+0000 INFO takes the value and converts it to string."
ALL GOOD SO FAR, but now I want to run it as loop, to print the messages. I made the following loop:
foreach($json_decoded['hits']['hits'] as $host) {
echo $host[$index]['_source']['message']['0'];
$index++;
}
I don't get any output. What is the problem?
When you foreach over ['hits']['hits'] this will iterate over the next level of the array (so the ['0'] and ['1'] elements). So you don't need to add in the [$index] part of your echo or the $index value...
foreach($json_decoded['hits']['hits'] as $host) {
echo $host['_source']['message']['0'];
}
I have the following array:
Array (
[result] => Array (
[id] => 58fba3ebf4
[type] => A
[name] => ser.domain.com
[content] => 192.168.100.1
[proxiable] =>
[proxied] =>
[ttl] => 1
[priority] => 10
[locked] =>
[zone_id] => eb0d86828e3ac837c
[zone_name] => domain.com
[modified_on] => 2018-07-06T06:37:14.069598Z
[created_on] => 2018-07-06T06:37:14.069598Z
[meta] => Array (
[auto_added] =>
[managed_by_apps] =>
[managed_by_argo_tunnel] =>
)
)
[success] => 1
[errors] => Array ( )
[messages] => Array ( )
)
How can I just get the value from id?
$data =json_decode($response);
$id = $data->result->id;
Assuming your payload from the request was $response.
You already have a multi-dimensional array so just try like this
echo $arrays['result']['id']; // this will return id
echo $arrays['result']['type']; // this will return type
Here is solution
// $result_array() is your reponse from curl PHP
$data = $result_array();
$id = $data['result']['id'];
I have an xml response for a DHL tracking and i want to echo specific elements on my php page.
I use the following code to print out the tracking results without formatting:
print_r($response);
The xml response looks like this:
Array
(
[TrackingResponse] => Array
(
[xmlns:req] => http://www.dhl.com
[xmlns:xsi] => http://www.w3.org/2001/XMLSchema-instance
[xsi:schemaLocation] => http://www.dhl.com TrackingResponse.xsd
[Response] => Array
(
[ServiceHeader] => Array
(
[MessageTime] => 2013-12-12T11:51:05+00:00
[MessageReference] => j2xfhcBpCE2yd9gbeC5tjqxIX8xjDpZ1
[SiteID] => iraqnova
)
)
[AWBInfo] => Array
(
[AWBNumber] => 8564385550
[Status] => Array
(
[ActionStatus] => success
)
[ShipmentInfo] => Array
(
[OriginServiceArea] => Array
(
[ServiceAreaCode] => FRA
[Description] => FRANKFURT - GERMANY
)
[DestinationServiceArea] => Array
(
[ServiceAreaCode] => MLA
[Description] => MALTA - MALTA
)
[ShipperName] => STANDARD CHARTERED BANK
[ShipperAccountNumber] => 144193851
[ConsigneeName] => BANK OF VALLETTA P.L.C
[ShipmentDate] => 2013-02-14T15:14:00
[Pieces] => 1
[Weight] => 0.08
[WeightUnit] => K
[GlobalProductCode] => U
[ShipmentDesc] => 1402130018
[DlvyNotificationFlag] => Y
[Shipper] => Array
(
[City] => Frankfurt/Main
[PostalCode] => 60486
[CountryCode] => DE
)
[Consignee] => Array
(
[City] => Santa Venera
[PostalCode] => 9030
[CountryCode] => MT
)
[ShipperReference] => Array
(
[ReferenceID] => Doc
)
)
)
)
)
I'm getting lost with so many foreach loops to get to the specific xml tags inside the [ShipmentInfo] tag:
foreach($response as $tag){
echo $tag['ShipmentInfo'];
}
The sample tracking number and info, from the DHL XML Service Validation website http://xmlpitest-ea.dhl.com/serviceval/jsps/main/Main_menu.jsp
Thank you,
$arr['TrackingResponse']['AWBInfo']['ShipmentInfo'] will lead to the shipment info and then iterate over this using foreach.
Like -
if(is_array($arr['TrackingResponse']['AWBInfo']['ShipmentInfo'])) {
foreach(is_array($arr['TrackingResponse']['AWBInfo']['ShipmentInfo']) as $shiptagkey=>$shiptagval)
{
echo $shiptagkey, " ", $shiptagval;
}
}
Although $shiptagval itself going to be an array so you need to care about this as well.
print_r($response ['TrackingResponse']['AWBInfo']['ShipmentInfo']);
or if you don't know the path: (pseudocode)
function findAndEcho($obj,$tag) {
for $obj as $key => $val {
if $key = $tag {
print_r($val)
}
else if is_array($val) {
findAndEcho($val,$tag)
}
}
}
I have the following array structure, this is the array passed in to $response
Array
(
[inbox] => Array
(
[0] => Array
(
[location] => 3
[ID] => 8ba84195fe79a89af1a4b5bd8c280621
[smsc_number] => +44******
[sent] => 2013-02-25 14:57:20
[coding] => Default GSM alphabet (no compression)
[remote_number] => +447****
[status] => Read
[body] => Yeppp
)
)
[outbox] => Array
(
[0] => Array
(
[location] => 2
[ID] => d22c4368fadd64e98fab64acb6b8fa34
[reference_number] => 1
[class] => 1
[coding] => Default GSM alphabet (no compression)
[remote_number] => *****
[status] => Sent
[body] => Test
)
[1] => Array
(
[location] => 0
[ID] => f0c05e8dd2578d16d73bf5dbcf2ec3e6
[class] => 1
[coding] => Default GSM alphabet (no compression)
[remote_number] => 0****
[status] => UnSent
[body] => fdgg ddfgfdg fd
)
[2] => Array
(
[location] => 1
[ID] => d7537acb1b3994ecc11369bac46c4bb6
[class] => 1
[coding] => Default GSM alphabet (no compression)
[remote_number] => 0****3
[status] => UnSent
[body] => fdgg ddfgfdg fd
)
)
)
I'm only interested in the body of the inbox array. I thought I could just do two loops to get this, or just do $response[0] but it doesn't seem to work. Heres what I have:
$response = $sms->Get();
foreach ($response[0] as $value) {
foreach ($response as $value1) {
echo($value1['body']);
}
}
I'm obviously doing something very stupid - Any help would be really appreciated
Try this
foreach ($response['inbox'] as $inb) {
echo($inb['body']); }