Creating a Table From Specific PHP Array Keys

brsy

nah mang
May 12, 2011
1,530
272
I have a PHP array with ~10 keys, each having their own values. There are ~5 entries of each of the ~10 keys, meaning I have ~50 keys in total. How can I create a table like this:
img.png


How can I create this using PHP? Here's what I have now, which just puts all the information into a table. So basically, how do I dissect it into Dates and ItemDescriptions? Here's the code that I'm currently using.
PHP:
<?php

$jSON = '{
   "LabelNumber":"oa0021148274",
   "Scans":[
      {
         "Type":"P",
         "Courier":"020",
         "Description":"Picked up",
         "Date":"06/11/2014 18:58:14",
         "Name":"Cape Town",
         "Franchise":"CPT",
         "Status":"PPP",
         "StatusDescription":"Your parcel was picked up",
         "CompanyInfo":{
            "contactName":"",
            "company":"",
            "address1":"",
            "address2":"",
            "address3":"",
            "address4":"",
            "address5":"",
            "address6":"",
            "address7":"",
            "address8":"",
            "comment":""
         },
         "UploadDate":"07/11/2014",
         "Signature":""
      },
      {
         "Type":"T",
         "Courier":"020",
         "Description":"Pretoria",
         "Date":"06/11/2014 19:04:30",
         "Name":"Cape Town",
         "Franchise":"CPT",
         "Status":"PRY",
         "StatusDescription":"Your parcel is currently in transit between our Cape Town and Pretoria depots.",
         "CompanyInfo":{
            "contactName":"",
            "company":"",
            "address1":"",
            "address2":"",
            "address3":"",
            "address4":"",
            "address5":"",
            "address6":"",
            "address7":"",
            "address8":"",
            "comment":""
         },
         "UploadDate":"07/11/2014",
         "Signature":""
      },
      {
         "Type":"T",
         "Courier":"011",
         "Description":"Pretoria Depot Scan           ",
         "Date":"10/11/2014 06:56:34",
         "Name":"Pretoria",
         "Franchise":"PRY",
         "Status":"O01",
         "StatusDescription":"Your parcel has been received in the Pretoria depot.",
         "CompanyInfo":{
            "contactName":"",
            "company":"",
            "address1":"",
            "address2":"",
            "address3":"",
            "address4":"",
            "address5":"",
            "address6":"",
            "address7":"",
            "address8":"",
            "comment":""
         },
         "UploadDate":"10/11/2014",
         "Signature":""
      },
      {
         "Type":"T",
         "Courier":"013",
         "Description":"Onboard",
         "Date":"10/11/2014 07:51:19",
         "Name":"Pretoria",
         "Franchise":"PRY",
         "Status":"ONB",
         "StatusDescription":"Onboard - the parcel is onboard the Courier vehicle.",
         "CompanyInfo":{
            "contactName":"",
            "company":"Home Delivery",
            "address1":"44 suikerbos",
            "address2":"lydiana",
            "address3":"o",
            "address4":"",
            "address5":"",
            "address6":"",
            "address7":"",
            "address8":"",
            "comment":""
         },
         "UploadDate":"10/11/2014",
         "Signature":""
      },
      {
         "Type":"D",
         "Courier":"013",
         "Description":"Signature Obtained",
         "Date":"10/11/2014 09:47:05",
         "Name":"Pretoria",
         "Franchise":"PRY",
         "Status":"YES",
         "StatusDescription":"Signature Obtained - Your parcel has been delivered and signed for.",
         "CompanyInfo":{
            "contactName":"philimon",
            "company":"Home Delivery",
            "address1":"44 suikerbos",
            "address2":"lydiana",
            "address3":"o",
            "address4":"",
            "address5":"",
            "address6":"",
            "address7":"",
            "address8":"",
            "comment":""
         },
         "UploadDate":"10/11/2014",
         "Signature":"http://api.fastway.org/signatures/0312408478f58b8b1d685a148089960f.jpg"
      }
   ],
   "Signature":"http://api.fastway.org/signatures/0312408478f58b8b1d685a148089960f.jpg",
   "DistributedTo":"Cape Town                                                   ",
   "DistributedDate":"8/08/2014",
   "Reference":"",
   "IsOnforward":"0"
}';

$trackingInfo = json_decode($jSON, true);

foreach($trackingInfo['Scans'] as $key => $value)
{
  
           echo "<table>";
       foreach ($value as $key2 => $value2)
       {
           echo "<tr><td>". $key2 ."</td>";
           echo "<td>". $value2 ."</td></tr>";
       }
           echo "</table>";

       echo ")";
}
?>
 
UPDATE.

I managed to figure out how to get individual values, but I have to now figure out how to make the table.

PHP:
foreach($trackingInfo['Scans'] as $key => $value) {
            $count = count($trackingInfo['Scans']);
           $dates[$count] = $value['Date'] . "<br>";
               print_r($dates);
}
 
UPDATE:
I fixed it.
PHP:
$getLabel = htmlspecialchars($_GET['label']);
$jSON = file_get_contents('http://api.fastway.org/v2/tracktrace/detail/'. $getLabel .'?api_key=7e54d8c9fc8ca8ea2e5ea057eb85c4c6');
/*$jSON = '{
   "result":{
      "LabelNumber":"oa0021148274",
      "Scans":[
         {
            "Type":"P",
            "Courier":"020",
            "Description":"Picked up",
            "Date":"06/11/2014 18:58:14",
            "Name":"Cape Town",
            "Franchise":"CPT",
            "Status":"PPP",
            "StatusDescription":"Your parcel was picked up",
            "CompanyInfo":{
               "contactName":"",
               "company":"",
               "address1":"",
               "address2":"",
               "address3":"",
               "address4":"",
               "address5":"",
               "address6":"",
               "address7":"",
               "address8":"",
               "comment":""
            },
            "UploadDate":"07/11/2014",
            "Signature":""
         },
         {
            "Type":"T",
            "Courier":"020",
            "Description":"Pretoria",
            "Date":"06/11/2014 19:04:30",
            "Name":"Cape Town",
            "Franchise":"CPT",
            "Status":"PRY",
            "StatusDescription":"Your parcel is currently in transit between our Cape Town and Pretoria depots.",
            "CompanyInfo":{
               "contactName":"",
               "company":"",
               "address1":"",
               "address2":"",
               "address3":"",
               "address4":"",
               "address5":"",
               "address6":"",
               "address7":"",
               "address8":"",
               "comment":""
            },
            "UploadDate":"07/11/2014",
            "Signature":""
         },
         {
            "Type":"T",
            "Courier":"011",
            "Description":"Pretoria Depot Scan           ",
            "Date":"10/11/2014 06:56:34",
            "Name":"Pretoria",
            "Franchise":"PRY",
            "Status":"O01",
            "StatusDescription":"Your parcel has been received in the Pretoria depot.",
            "CompanyInfo":{
               "contactName":"",
               "company":"",
               "address1":"",
               "address2":"",
               "address3":"",
               "address4":"",
               "address5":"",
               "address6":"",
               "address7":"",
               "address8":"",
               "comment":""
            },
            "UploadDate":"10/11/2014",
            "Signature":""
         },
         {
            "Type":"T",
            "Courier":"013",
            "Description":"Onboard",
            "Date":"10/11/2014 07:51:19",
            "Name":"Pretoria",
            "Franchise":"PRY",
            "Status":"ONB",
            "StatusDescription":"Onboard - the parcel is onboard the Courier vehicle.",
            "CompanyInfo":{
               "contactName":"",
               "company":"Home Delivery",
               "address1":"44 suikerbos",
               "address2":"lydiana",
               "address3":"o",
               "address4":"",
               "address5":"",
               "address6":"",
               "address7":"",
               "address8":"",
               "comment":""
            },
            "UploadDate":"10/11/2014",
            "Signature":""
         },
         {
            "Type":"D",
            "Courier":"013",
            "Description":"Signature Obtained",
            "Date":"10/11/2014 09:47:05",
            "Name":"Pretoria",
            "Franchise":"PRY",
            "Status":"YES",
            "StatusDescription":"Signature Obtained - Your parcel has been delivered and signed for.",
            "CompanyInfo":{
               "contactName":"philimon",
               "company":"Home Delivery",
               "address1":"44 suikerbos",
               "address2":"lydiana",
               "address3":"o",
               "address4":"",
               "address5":"",
               "address6":"",
               "address7":"",
               "address8":"",
               "comment":""
            },
            "UploadDate":"10/11/2014",
            "Signature":"http://api.fastway.org/signatures/0312408478f58b8b1d685a148089960f.jpg"
         }
      ],
      "Signature":"http://api.fastway.org/signatures/0312408478f58b8b1d685a148089960f.jpg",
      "DistributedTo":"Cape Town",
      "DistributedDate":"8/08/2014",
      "Reference":"",
      "IsOnforward":"0"
   },
   "generated_in":"187ms"
}';*/


@$trackingInfo = json_decode($jSON, true);
if(strpos($jSON, 'does not have any scans') === false) {
    if(isset($jSON) && !empty($jSON)) {
    echo "
        <style>
            td {
                padding: 7px 15px 7px 15px;
            }
            tr:nth-child(even) {
                background-color: #ccc;
            }
            tr:nth-child(odd) {
                background-color: #fff;
            }
        </style>
    ";
       
    echo "<h2>Tracking Number:<span style='font-weight:normal'> ". $trackingInfo['result']['LabelNumber'] ."</span></h2>";
    echo "<table style='border: 1px solid #ccc;'>";
    echo "<tr><td><h4>Date: </h4></td>";
    echo "<td><h4>Status: </h4></tr>";
   
    foreach($trackingInfo['result']['Scans'] as $key => $value) {
        $count = count($trackingInfo['Scans']);
       
        $dates[$count] = $value['Date'];
        $desc[$count]  = $value['StatusDescription'];
        $signature[$count] = $value['Signature'];
       
        echo "<tr><td>". $dates[$count] ."</td>";
        echo "<td>". $desc[$count] ."</td></tr>";
       
        if(!empty($signature[$count])) {
            $siggy = $signature[$count];
            echo "<tr><td>Signature</td><td><img src='". $siggy ."' alt='signature' /></td></tr>";
        }
    }
    echo "</table>";
} else {
        echo "<h2>POST data not found. Please resubmit the form.</h2>";
        return false;
    }
} else {
    echo "<h2>The label you have used does not exist. If you believe this is an error, please contact us.</h2>";
    return false;
}
 
Last edited:

Sysode

Front-End Developer
Dec 11, 2012
1,673
848
I have a PHP array with ~10 keys, each having their own values. There are ~5 entries of each of the ~10 keys, meaning I have ~50 keys in total. How can I create a table like this:
img.png


How can I create this using PHP? Here's what I have now, which just puts all the information into a table. So basically, how do I dissect it into Dates and ItemDescriptions? Here's the code that I'm currently using.
PHP:
<?php

$jSON = '{
   "LabelNumber":"oa0021148274",
   "Scans":[
      {
         "Type":"P",
         "Courier":"020",
         "Description":"Picked up",
         "Date":"06/11/2014 18:58:14",
         "Name":"Cape Town",
         "Franchise":"CPT",
         "Status":"PPP",
         "StatusDescription":"Your parcel was picked up",
         "CompanyInfo":{
            "contactName":"",
            "company":"",
            "address1":"",
            "address2":"",
            "address3":"",
            "address4":"",
            "address5":"",
            "address6":"",
            "address7":"",
            "address8":"",
            "comment":""
         },
         "UploadDate":"07/11/2014",
         "Signature":""
      },
      {
         "Type":"T",
         "Courier":"020",
         "Description":"Pretoria",
         "Date":"06/11/2014 19:04:30",
         "Name":"Cape Town",
         "Franchise":"CPT",
         "Status":"PRY",
         "StatusDescription":"Your parcel is currently in transit between our Cape Town and Pretoria depots.",
         "CompanyInfo":{
            "contactName":"",
            "company":"",
            "address1":"",
            "address2":"",
            "address3":"",
            "address4":"",
            "address5":"",
            "address6":"",
            "address7":"",
            "address8":"",
            "comment":""
         },
         "UploadDate":"07/11/2014",
         "Signature":""
      },
      {
         "Type":"T",
         "Courier":"011",
         "Description":"Pretoria Depot Scan           ",
         "Date":"10/11/2014 06:56:34",
         "Name":"Pretoria",
         "Franchise":"PRY",
         "Status":"O01",
         "StatusDescription":"Your parcel has been received in the Pretoria depot.",
         "CompanyInfo":{
            "contactName":"",
            "company":"",
            "address1":"",
            "address2":"",
            "address3":"",
            "address4":"",
            "address5":"",
            "address6":"",
            "address7":"",
            "address8":"",
            "comment":""
         },
         "UploadDate":"10/11/2014",
         "Signature":""
      },
      {
         "Type":"T",
         "Courier":"013",
         "Description":"Onboard",
         "Date":"10/11/2014 07:51:19",
         "Name":"Pretoria",
         "Franchise":"PRY",
         "Status":"ONB",
         "StatusDescription":"Onboard - the parcel is onboard the Courier vehicle.",
         "CompanyInfo":{
            "contactName":"",
            "company":"Home Delivery",
            "address1":"44 suikerbos",
            "address2":"lydiana",
            "address3":"o",
            "address4":"",
            "address5":"",
            "address6":"",
            "address7":"",
            "address8":"",
            "comment":""
         },
         "UploadDate":"10/11/2014",
         "Signature":""
      },
      {
         "Type":"D",
         "Courier":"013",
         "Description":"Signature Obtained",
         "Date":"10/11/2014 09:47:05",
         "Name":"Pretoria",
         "Franchise":"PRY",
         "Status":"YES",
         "StatusDescription":"Signature Obtained - Your parcel has been delivered and signed for.",
         "CompanyInfo":{
            "contactName":"philimon",
            "company":"Home Delivery",
            "address1":"44 suikerbos",
            "address2":"lydiana",
            "address3":"o",
            "address4":"",
            "address5":"",
            "address6":"",
            "address7":"",
            "address8":"",
            "comment":""
         },
         "UploadDate":"10/11/2014",
         "Signature":"http://api.fastway.org/signatures/0312408478f58b8b1d685a148089960f.jpg"
      }
   ],
   "Signature":"http://api.fastway.org/signatures/0312408478f58b8b1d685a148089960f.jpg",
   "DistributedTo":"Cape Town                                                   ",
   "DistributedDate":"8/08/2014",
   "Reference":"",
   "IsOnforward":"0"
}';

$trackingInfo = json_decode($jSON, true);

foreach($trackingInfo['Scans'] as $key => $value)
{
  
           echo "<table>";
       foreach ($value as $key2 => $value2)
       {
           echo "<tr><td>". $key2 ."</td>";
           echo "<td>". $value2 ."</td></tr>";
       }
           echo "</table>";

       echo ")";
}
?>
 
UPDATE.

I managed to figure out how to get individual values, but I have to now figure out how to make the table.

PHP:
foreach($trackingInfo['Scans'] as $key => $value) {
            $count = count($trackingInfo['Scans']);
           $dates[$count] = $value['Date'] . "<br>";
               print_r($dates);
}
 
UPDATE:
Actually, I solved it. Sorry for posting this thread, it has been a long day and I went brain dead when I posted it.
Please post your fix or else this thread is useless and may as well be removed.
 

Users who are viewing this thread

Top