I have really been struggling with outputting the content of a json data I received from a rest API response correctly. I have searched a lot and tried different ways but I noticed everyone's is always unique...below is my code..specifically of consideration is the "headers_field" it is an array with a lot of values and keys...and I want to also output these values with the keys...While the ("data") has two values with it...below is what I have tried also but I am just getting the first result from the loop while it returns ("headers_field": Array), ("box: Array")..I used Curl. Kindly look below. Thanks.
{
"data": [
{
"approved": null,
"approved_user_id": null,
"boxes": [],
"created": "2020-05-19T21",
"created_at": "2020-05-19T21",
"edited": null,
"edited_in_affiliate": null,
"edited_in_hdr": null,
"edited_in_web_app": null,
"erp_project_id": null,
"error_description": "",
"header_fields": [
{
"boxes": [],
"code": "creditor_number",
"data_type": "",
"error": "",
"feature_id": nvnnn,
"value": "hhhhh"
},
{
"boxes": [],
"code": "payment_account_number",
"data_type": "",
"error": "",
"value": ""
},
{
"boxes": [],
"code": "order_number",
"data_type": "",
"error": "",
"value": ""
},
{
"boxes": [],
"code": "total_amount_incl_vat",
"data_type": "",
"error": "",
"feature_id": mmm
"value": "hhh"
},
{
"approved": null,
"approved_user_id": null,
"boxes": [],
"created": "2020-05-19",
"created_at": "2020-05-19",
"edited": null,
"edited_in_affiliate": null,
"edited_in_hdr": null,
"edited_in_web_app": null,
"erp_project_id": null,
"error_description": "",
"header_fields": [
{
"boxes": [],
"code": "reference",
"data_type": "",
"error": "",
"value": ""
},
{
"boxes": [],
"code": "payment_swift_bic",
"data_type": "",
"error": "",
"value": ""
},
{
"boxes": [],
"code": "recipient_vat_reg_no",
"data_type": "",
"error": "",
"value": ""
},
{
"boxes": [],
"code": "joint_payment_id",
"data_type": "",
"error": "",
"value": ""
},
{
"boxes": [],
"code": "catalog_debitor_id",
"data_type": "",
"error": "",
"value": ""
},
{
"boxes": [],
"code": "case_number",
"data_type": "",
"error": "",
"value": ""
},
{
"boxes": [],
"code": "payment_account_number",
"data_type": "",
"error": "",
"value": ""
},
{
"boxes": [],
"code": "payment_iban",
"data_type": "",
"error": "",
"value": ""
}
],
"id": 00000,
"image": "https://skkkkkllllllllll
"line_items": [],
"note": "",
"organization_id": 11111,
"organization_voucher_id": 7777,
"page_count": 1,
"status": "successful",
"type": "voucher",
"upload_type": "affiliate",
"uploaded_by_user_id": 0000001
"user_payment_method": "",
"user_voucher_type": ""
}
],
"meta": {
"count": 2
}
}
MY LOOP OPERATION
.....
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$results= curl_exec($ch);
$g= json_decode($results, true);
$i=0;
$g_length =count($g["data"]);//to count through the child array
for ($i=0; $i < ($g_length);$i++)
{
$k= $g["data"][$i]; //asssigning the array position to a variable
foreach($k as $mydata=>$mvalue) {
$hey = trim($mvalue); //Removes white spaces
if ($mvalue !=="")
echo $mydata. " : " . $mvalue . "<br>";
else
echo "This is empty. <br/>";
///////////////////////////////////////////
foreach($mvalue as $lkey=>$lvalue)
{
$u=is_array($mvalue);
if(($u) and !empty($u))
echo $lkey. ":" . $lvalue. "<br>";
else
echo "what? <br/>";
///////////
foreach($lvalue as $okey=>$ovalue)
{
echo $okey. ":". $value. "</n>";
/////
foreach($ovalue as $key=>$value)
{
$v=is_array($value);
if(($v) and !empty($v))
echo $key. ":" . $value. "<br>";
else
echo "hello";
//print_r($key);
/////
}
}
//////////
}
//////////////////////////////////////////
}