-1

I have data in PHP variable.

$data = '[{
    "ConsignmentNo": "07438-01-010001533",
    "ConsigneeName": "Qaiser  Siraj ",
    "ConsigneeAddress": "House 133 Street 33 sector G 11_2  u     140        tr     2138877  ",
    "ConsigneeCity": "ISLAMABAD",
    "ContactNo": "03335194126",
    "ShipperName": "Chase Value Centre",
    "ShipperAddress": "Chase Value Centre, Plot # 184, Sector 23, Near Brooks Chowrangi, Korangi Industrial Area, Karachi.",
    "TransactionDate": "2020-12-10T00:00:00",
    "OperationDesc": "Booked",
    "ProcessDescForPortal": "CONSIGNMENT BOOKED",
    "ReceiverName": "",
    "Relation": "",
    "ReasonDesc": "",
    "IsPortalBooking": "YES",
    "HomeBranch": "KHI",
    "DestBranch": "RWP",
    "codAmount": 898.0,
    "Weight": 0.69,
    "Pcs": 1.0,
    "ServiceType": "COD",
    "OriginCity": "KARACHI",
    "MDNNo": "COD",
    "CallDate": "2021-01-18T14:49:14.06",
    "CallTime": "2021-01-18T14:49:14.06",
    "CallStatus": null,
    "CallRemarks": null
}, {
    "ConsignmentNo": "07438-01-010001533",
    "ConsigneeName": "Qaiser  Siraj ",
    "ConsigneeAddress": "House 133 Street 33 sector G 11_2  u     140        tr     2138877  ",
    "ConsigneeCity": "ISLAMABAD",
    "ContactNo": "03335194126",
    "ShipperName": "Chase Value Centre",
    "ShipperAddress": "Chase Value Centre, Plot # 184, Sector 23, Near Brooks Chowrangi, Korangi Industrial Area, Karachi.",
    "TransactionDate": "2020-12-11T00:00:00",
    "OperationDesc": "Shipment Arrived",
    "ProcessDescForPortal": "ARRIVED AT ORIGIN BRANCH",
    "ReceiverName": "",
    "Relation": "",
    "ReasonDesc": "",
    "IsPortalBooking": "YES",
    "HomeBranch": "KHI",
    "DestBranch": "",
    "codAmount": 898.0,
    "Weight": 0.69,
    "Pcs": 1.0,
    "ServiceType": "COD",
    "OriginCity": "KARACHI",
    "MDNNo": "COD",
    "CallDate": "2021-01-18T14:49:14.06",
    "CallTime": "2021-01-18T14:49:14.06",
    "CallStatus": null,
    "CallRemarks": null
}, {
    "ConsignmentNo": "07438-01-010001533",
    "ConsigneeName": "Qaiser  Siraj ",
    "ConsigneeAddress": "House 133 Street 33 sector G 11_2  u     140        tr     2138877  ",
    "ConsigneeCity": "ISLAMABAD",
    "ContactNo": "03335194126",
    "ShipperName": "Chase Value Centre",
    "ShipperAddress": "Chase Value Centre, Plot # 184, Sector 23, Near Brooks Chowrangi, Korangi Industrial Area, Karachi.",
    "TransactionDate": "2020-12-12T03:10:16.743",
    "OperationDesc": "Forward",
    "ProcessDescForPortal": "MOVED TO DEST. BRANCH",
    "ReceiverName": "",
    "Relation": "",
    "ReasonDesc": "",
    "IsPortalBooking": "YES",
    "HomeBranch": "KHI",
    "DestBranch": "LAHORE",
    "codAmount": 898.0,
    "Weight": 0.69,
    "Pcs": 1.0,
    "ServiceType": "COD",
    "OriginCity": "KARACHI",
    "MDNNo": "COD",
    "CallDate": "2021-01-18T14:49:14.06",
    "CallTime": "2021-01-18T14:49:14.06",
    "CallStatus": null,
    "CallRemarks": null
}, {
    "ConsignmentNo": "07438-01-010001533",
    "ConsigneeName": "Qaiser  Siraj ",
    "ConsigneeAddress": "House 133 Street 33 sector G 11_2  u     140        tr     2138877  ",
    "ConsigneeCity": "ISLAMABAD",
    "ContactNo": "03335194126",
    "ShipperName": "Chase Value Centre",
    "ShipperAddress": "Chase Value Centre, Plot # 184, Sector 23, Near Brooks Chowrangi, Korangi Industrial Area, Karachi.",
    "TransactionDate": "2020-12-13T04:59:14.783",
    "OperationDesc": "Forward",
    "ProcessDescForPortal": "MOVED TO DEST. BRANCH",
    "ReceiverName": "",
    "Relation": "",
    "ReasonDesc": "",
    "IsPortalBooking": "YES",
    "HomeBranch": "LHE",
    "DestBranch": "ISLAMABAD",
    "codAmount": 898.0,
    "Weight": 0.69,
    "Pcs": 1.0,
    "ServiceType": "COD",
    "OriginCity": "KARACHI",
    "MDNNo": "COD",
    "CallDate": "2021-01-18T14:49:14.06",
    "CallTime": "2021-01-18T14:49:14.06",
    "CallStatus": null,
    "CallRemarks": null
}, {
    "ConsignmentNo": "07438-01-010001533",
    "ConsigneeName": "Qaiser  Siraj ",
    "ConsigneeAddress": "House 133 Street 33 sector G 11_2  u     140        tr     2138877  ",
    "ConsigneeCity": "ISLAMABAD",
    "ContactNo": "03335194126",
    "ShipperName": "Chase Value Centre",
    "ShipperAddress": "Chase Value Centre, Plot # 184, Sector 23, Near Brooks Chowrangi, Korangi Industrial Area, Karachi.",
    "TransactionDate": "2020-12-13T11:14:02.76",
    "OperationDesc": "DeManifested",
    "ProcessDescForPortal": "REACHED AT DEST. BRANCH",
    "ReceiverName": "",
    "Relation": "",
    "ReasonDesc": "",
    "IsPortalBooking": "YES",
    "HomeBranch": "RWP",
    "DestBranch": "",
    "codAmount": 898.0,
    "Weight": 0.69,
    "Pcs": 1.0,
    "ServiceType": "COD",
    "OriginCity": "KARACHI",
    "MDNNo": "COD",
    "CallDate": "2021-01-18T14:49:14.06",
    "CallTime": "2021-01-18T14:49:14.06",
    "CallStatus": null,
    "CallRemarks": null
}, {
    "ConsignmentNo": "07438-01-010001533",
    "ConsigneeName": "Qaiser  Siraj ",
    "ConsigneeAddress": "House 133 Street 33 sector G 11_2  u     140        tr     2138877  ",
    "ConsigneeCity": "ISLAMABAD",
    "ContactNo": "03335194126",
    "ShipperName": "Chase Value Centre",
    "ShipperAddress": "Chase Value Centre, Plot # 184, Sector 23, Near Brooks Chowrangi, Korangi Industrial Area, Karachi.",
    "TransactionDate": "2020-12-14T05:45:00",
    "OperationDesc": "Dlvry Phase I",
    "ProcessDescForPortal": "OUT FOR DELIVERY",
    "ReceiverName": "",
    "Relation": "",
    "ReasonDesc": "",
    "IsPortalBooking": "YES",
    "HomeBranch": "RWP",
    "DestBranch": "RWP",
    "codAmount": 898.0,
    "Weight": 0.69,
    "Pcs": 1.0,
    "ServiceType": "COD",
    "OriginCity": "KARACHI",
    "MDNNo": "COD",
    "CallDate": "2021-01-18T14:49:14.06",
    "CallTime": "2021-01-18T14:49:14.06",
    "CallStatus": null,
    "CallRemarks": null
}, {
    "ConsignmentNo": "07438-01-010001533",
    "ConsigneeName": "Qaiser  Siraj ",
    "ConsigneeAddress": "House 133 Street 33 sector G 11_2  u     140        tr     2138877  ",
    "ConsigneeCity": "ISLAMABAD",
    "ContactNo": "03335194126",
    "ShipperName": "Chase Value Centre",
    "ShipperAddress": "Chase Value Centre, Plot # 184, Sector 23, Near Brooks Chowrangi, Korangi Industrial Area, Karachi.",
    "TransactionDate": "2020-12-14T23:09:39.02",
    "OperationDesc": "Dlvry Phase II",
    "ProcessDescForPortal": "UNDELIVERED",
    "ReceiverName": "CONTACT NOT ESTABLISHED DELIVERY NOT ARRANGED",
    "Relation": "null",
    "ReasonDesc": "CNE",
    "IsPortalBooking": "YES",
    "HomeBranch": "RWP",
    "DestBranch": "RWP",
    "codAmount": 898.0,
    "Weight": 0.69,
    "Pcs": 1.0,
    "ServiceType": "COD",
    "OriginCity": "KARACHI",
    "MDNNo": "COD",
    "CallDate": "2021-01-18T14:49:14.06",
    "CallTime": "2021-01-18T14:49:14.06",
    "CallStatus": null,
    "CallRemarks": null
}, {
    "ConsignmentNo": "07438-01-010001533",
    "ConsigneeName": "Qaiser  Siraj ",
    "ConsigneeAddress": "House 133 Street 33 sector G 11_2  u     140        tr     2138877  ",
    "ConsigneeCity": "ISLAMABAD",
    "ContactNo": "03335194126",
    "ShipperName": "Chase Value Centre",
    "ShipperAddress": "Chase Value Centre, Plot # 184, Sector 23, Near Brooks Chowrangi, Korangi Industrial Area, Karachi.",
    "TransactionDate": "2020-12-15T05:45:00",
    "OperationDesc": "Dlvry Phase I",
    "ProcessDescForPortal": "OUT FOR DELIVERY",
    "ReceiverName": "",
    "Relation": "",
    "ReasonDesc": "",
    "IsPortalBooking": "YES",
    "HomeBranch": "RWP",
    "DestBranch": "RWP",
    "codAmount": 898.0,
    "Weight": 0.69,
    "Pcs": 1.0,
    "ServiceType": "COD",
    "OriginCity": "KARACHI",
    "MDNNo": "COD",
    "CallDate": "2021-01-18T14:49:14.06",
    "CallTime": "2021-01-18T14:49:14.06",
    "CallStatus": null,
    "CallRemarks": null
}, {
    "ConsignmentNo": "07438-01-010001533",
    "ConsigneeName": "Qaiser  Siraj ",
    "ConsigneeAddress": "House 133 Street 33 sector G 11_2  u     140        tr     2138877  ",
    "ConsigneeCity": "ISLAMABAD",
    "ContactNo": "03335194126",
    "ShipperName": "Chase Value Centre",
    "ShipperAddress": "Chase Value Centre, Plot # 184, Sector 23, Near Brooks Chowrangi, Korangi Industrial Area, Karachi.",
    "TransactionDate": "2020-12-15T19:01:25.31",
    "OperationDesc": "Dlvry Phase II",
    "ProcessDescForPortal": "DELIVERED",
    "ReceiverName": "QAISER",
    "Relation": "SELF",
    "ReasonDesc": "OK",
    "IsPortalBooking": "YES",
    "HomeBranch": "RWP",
    "DestBranch": "RWP",
    "codAmount": 898.0,
    "Weight": 0.69,
    "Pcs": 1.0,
    "ServiceType": "COD",
    "OriginCity": "KARACHI",
    "MDNNo": "COD",
    "CallDate": "2021-01-18T14:49:14.06",
    "CallTime": "2021-01-18T14:49:14.06",
    "CallStatus": null,
    "CallRemarks": null
}]';

I want to get the last status of shipment in a separate variable. I used this code

$status = $data[0]['ProcessDescForPortal'];

But it returns the first status of the result e.g "CONSIGNMENT BOOKED" but I want to save the last status that is "DELIVERED". Actually, I want to get the last status of the tracking.

How to achieve this?

Regards

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
Hannah James
  • 540
  • 3
  • 14

4 Answers4

0

You have to convert it to array by using json_decode() and then select the array and point to the object.

Here's the working code


$data = '[{"ConsignmentNo":"07438-01-010001533","ConsigneeName":"Qaiser  Siraj ","ConsigneeAddress":"House 133 Street 33 sector G 11_2  u     140        tr     2138877  ","ConsigneeCity":"ISLAMABAD","ContactNo":"03335194126","ShipperName":"Chase Value Centre","ShipperAddress":"Chase Value Centre, Plot # 184, Sector 23, Near Brooks Chowrangi, Korangi Industrial Area, Karachi.","TransactionDate":"2020-12-10T00:00:00","OperationDesc":"Booked","ProcessDescForPortal":"CONSIGNMENT BOOKED","ReceiverName":"","Relation":"","ReasonDesc":"","IsPortalBooking":"YES","HomeBranch":"KHI","DestBranch":"RWP","codAmount":898.0,"Weight":0.69,"Pcs":1.0,"ServiceType":"COD","OriginCity":"KARACHI","MDNNo":"COD","CallDate":"2021-01-18T14:49:14.06","CallTime":"2021-01-18T14:49:14.06","CallStatus":null,"CallRemarks":null},{"ConsignmentNo":"07438-01-010001533","ConsigneeName":"Qaiser  Siraj ","ConsigneeAddress":"House 133 Street 33 sector G 11_2  u     140        tr     2138877  ","ConsigneeCity":"ISLAMABAD","ContactNo":"03335194126","ShipperName":"Chase Value Centre","ShipperAddress":"Chase Value Centre, Plot # 184, Sector 23, Near Brooks Chowrangi, Korangi Industrial Area, Karachi.","TransactionDate":"2020-12-11T00:00:00","OperationDesc":"Shipment Arrived","ProcessDescForPortal":"ARRIVED AT ORIGIN BRANCH","ReceiverName":"","Relation":"","ReasonDesc":"","IsPortalBooking":"YES","HomeBranch":"KHI","DestBranch":"","codAmount":898.0,"Weight":0.69,"Pcs":1.0,"ServiceType":"COD","OriginCity":"KARACHI","MDNNo":"COD","CallDate":"2021-01-18T14:49:14.06","CallTime":"2021-01-18T14:49:14.06","CallStatus":null,"CallRemarks":null},{"ConsignmentNo":"07438-01-010001533","ConsigneeName":"Qaiser  Siraj ","ConsigneeAddress":"House 133 Street 33 sector G 11_2  u     140        tr     2138877  ","ConsigneeCity":"ISLAMABAD","ContactNo":"03335194126","ShipperName":"Chase Value Centre","ShipperAddress":"Chase Value Centre, Plot # 184, Sector 23, Near Brooks Chowrangi, Korangi Industrial Area, Karachi.","TransactionDate":"2020-12-12T03:10:16.743","OperationDesc":"Forward","ProcessDescForPortal":"MOVED TO DEST. BRANCH","ReceiverName":"","Relation":"","ReasonDesc":"","IsPortalBooking":"YES","HomeBranch":"KHI","DestBranch":"LAHORE","codAmount":898.0,"Weight":0.69,"Pcs":1.0,"ServiceType":"COD","OriginCity":"KARACHI","MDNNo":"COD","CallDate":"2021-01-18T14:49:14.06","CallTime":"2021-01-18T14:49:14.06","CallStatus":null,"CallRemarks":null},{"ConsignmentNo":"07438-01-010001533","ConsigneeName":"Qaiser  Siraj ","ConsigneeAddress":"House 133 Street 33 sector G 11_2  u     140        tr     2138877  ","ConsigneeCity":"ISLAMABAD","ContactNo":"03335194126","ShipperName":"Chase Value Centre","ShipperAddress":"Chase Value Centre, Plot # 184, Sector 23, Near Brooks Chowrangi, Korangi Industrial Area, Karachi.","TransactionDate":"2020-12-13T04:59:14.783","OperationDesc":"Forward","ProcessDescForPortal":"MOVED TO DEST. BRANCH","ReceiverName":"","Relation":"","ReasonDesc":"","IsPortalBooking":"YES","HomeBranch":"LHE","DestBranch":"ISLAMABAD","codAmount":898.0,"Weight":0.69,"Pcs":1.0,"ServiceType":"COD","OriginCity":"KARACHI","MDNNo":"COD","CallDate":"2021-01-18T14:49:14.06","CallTime":"2021-01-18T14:49:14.06","CallStatus":null,"CallRemarks":null},{"ConsignmentNo":"07438-01-010001533","ConsigneeName":"Qaiser  Siraj ","ConsigneeAddress":"House 133 Street 33 sector G 11_2  u     140        tr     2138877  ","ConsigneeCity":"ISLAMABAD","ContactNo":"03335194126","ShipperName":"Chase Value Centre","ShipperAddress":"Chase Value Centre, Plot # 184, Sector 23, Near Brooks Chowrangi, Korangi Industrial Area, Karachi.","TransactionDate":"2020-12-13T11:14:02.76","OperationDesc":"DeManifested","ProcessDescForPortal":"REACHED AT DEST. BRANCH","ReceiverName":"","Relation":"","ReasonDesc":"","IsPortalBooking":"YES","HomeBranch":"RWP","DestBranch":"","codAmount":898.0,"Weight":0.69,"Pcs":1.0,"ServiceType":"COD","OriginCity":"KARACHI","MDNNo":"COD","CallDate":"2021-01-18T14:49:14.06","CallTime":"2021-01-18T14:49:14.06","CallStatus":null,"CallRemarks":null},{"ConsignmentNo":"07438-01-010001533","ConsigneeName":"Qaiser  Siraj ","ConsigneeAddress":"House 133 Street 33 sector G 11_2  u     140        tr     2138877  ","ConsigneeCity":"ISLAMABAD","ContactNo":"03335194126","ShipperName":"Chase Value Centre","ShipperAddress":"Chase Value Centre, Plot # 184, Sector 23, Near Brooks Chowrangi, Korangi Industrial Area, Karachi.","TransactionDate":"2020-12-14T05:45:00","OperationDesc":"Dlvry Phase I","ProcessDescForPortal":"OUT FOR DELIVERY","ReceiverName":"","Relation":"","ReasonDesc":"","IsPortalBooking":"YES","HomeBranch":"RWP","DestBranch":"RWP","codAmount":898.0,"Weight":0.69,"Pcs":1.0,"ServiceType":"COD","OriginCity":"KARACHI","MDNNo":"COD","CallDate":"2021-01-18T14:49:14.06","CallTime":"2021-01-18T14:49:14.06","CallStatus":null,"CallRemarks":null},{"ConsignmentNo":"07438-01-010001533","ConsigneeName":"Qaiser  Siraj ","ConsigneeAddress":"House 133 Street 33 sector G 11_2  u     140        tr     2138877  ","ConsigneeCity":"ISLAMABAD","ContactNo":"03335194126","ShipperName":"Chase Value Centre","ShipperAddress":"Chase Value Centre, Plot # 184, Sector 23, Near Brooks Chowrangi, Korangi Industrial Area, Karachi.","TransactionDate":"2020-12-14T23:09:39.02","OperationDesc":"Dlvry Phase II","ProcessDescForPortal":"UNDELIVERED","ReceiverName":"CONTACT NOT ESTABLISHED DELIVERY NOT ARRANGED","Relation":"null","ReasonDesc":"CNE","IsPortalBooking":"YES","HomeBranch":"RWP","DestBranch":"RWP","codAmount":898.0,"Weight":0.69,"Pcs":1.0,"ServiceType":"COD","OriginCity":"KARACHI","MDNNo":"COD","CallDate":"2021-01-18T14:49:14.06","CallTime":"2021-01-18T14:49:14.06","CallStatus":null,"CallRemarks":null},{"ConsignmentNo":"07438-01-010001533","ConsigneeName":"Qaiser  Siraj ","ConsigneeAddress":"House 133 Street 33 sector G 11_2  u     140        tr     2138877  ","ConsigneeCity":"ISLAMABAD","ContactNo":"03335194126","ShipperName":"Chase Value Centre","ShipperAddress":"Chase Value Centre, Plot # 184, Sector 23, Near Brooks Chowrangi, Korangi Industrial Area, Karachi.","TransactionDate":"2020-12-15T05:45:00","OperationDesc":"Dlvry Phase I","ProcessDescForPortal":"OUT FOR DELIVERY","ReceiverName":"","Relation":"","ReasonDesc":"","IsPortalBooking":"YES","HomeBranch":"RWP","DestBranch":"RWP","codAmount":898.0,"Weight":0.69,"Pcs":1.0,"ServiceType":"COD","OriginCity":"KARACHI","MDNNo":"COD","CallDate":"2021-01-18T14:49:14.06","CallTime":"2021-01-18T14:49:14.06","CallStatus":null,"CallRemarks":null},{"ConsignmentNo":"07438-01-010001533","ConsigneeName":"Qaiser  Siraj ","ConsigneeAddress":"House 133 Street 33 sector G 11_2  u     140        tr     2138877  ","ConsigneeCity":"ISLAMABAD","ContactNo":"03335194126","ShipperName":"Chase Value Centre","ShipperAddress":"Chase Value Centre, Plot # 184, Sector 23, Near Brooks Chowrangi, Korangi Industrial Area, Karachi.","TransactionDate":"2020-12-15T19:01:25.31","OperationDesc":"Dlvry Phase II","ProcessDescForPortal":"DELIVERED","ReceiverName":"QAISER","Relation":"SELF","ReasonDesc":"OK","IsPortalBooking":"YES","HomeBranch":"RWP","DestBranch":"RWP","codAmount":898.0,"Weight":0.69,"Pcs":1.0,"ServiceType":"COD","OriginCity":"KARACHI","MDNNo":"COD","CallDate":"2021-01-18T14:49:14.06","CallTime":"2021-01-18T14:49:14.06","CallStatus":null,"CallRemarks":null}]';
$array = json_decode($data);
 
 foreach($array as $key => $value){
     
    echo($value->ProcessDescForPortal == "DELIVERED"? $value->ProcessDescForPortal:"");
     
 }
//output

"DELIVERED"
 
Jwan
  • 177
  • 3
  • 12
0

Use a map function to return only the the status, use the end function to return the last status

$data = json_decode($data,true);
$data = array_map($data,function($row) {
   return $row['ProcessDescForPortal'];
});
var_dump(end($data));
madalinivascu
  • 32,064
  • 4
  • 39
  • 55
0

Thank you, everyone, I figure out the answer

end($data)['ProcessDescForPortal'];

Regards.

Hannah James
  • 540
  • 3
  • 14
  • Yes :) @RobinSingh thank you :) – Hannah James Jan 18 '21 at 10:19
  • If the position of "ProcessDescForPortal" changes you won't notice it. So You have to use either my method or madalinivascu's method. PS never mind – Jwan Jan 18 '21 at 10:20
  • 1
    actually @HannahJames say's that he/she want to get the last element of ['ProcessDescForPortal'] no need check if it's delivered or not, the value of it's is not important but the position. – Jerson Jan 18 '21 at 10:22
  • @Jerson yes exactly :) But thank you everyone and StackOverflow for quick response :) – Hannah James Jan 18 '21 at 10:24
0

You have two options.

  • The array is always ordered by TransactionDate property ascending. Then you just need to take the last item in the array.
$arr = json_decode($data);
$last = end($arr);
$ProcessDescForPortal = $last->ProcessDescForPortal;
  • The array is not mandatory ordered. Then you will need to sort the array first and then use the latest.
$arr = json_decode($data);
usort($arr, function($a, $b){
    return strcmp($a->TransactionDate, $b->TransactionDate);
});
$last = end($arr);
$ProcessDescForPortal = $last->ProcessDescForPortal;
Abkarino
  • 1,426
  • 1
  • 12
  • 19
  • OP said that he want to get the last position value of ['ProcessDescForPortal'] not the whole array – Jerson Jan 18 '21 at 10:24
  • Fixed. Thanks! I think the most important part of the answer is how to get the correct element to extract `ProcessDescForPortal` from. – Abkarino Jan 18 '21 at 10:26