-1

I need to parse a JSON Array Response in PHP and echo the values in an HTML Table. I'm very new to PHP-JSON, I'm unable to figure out a way to do so.

$curl_response = curl_exec($curl);
echo $curl_response . PHP_EOL;

JSON Array Response :

{
    "employees": [
        {
            "emp_id":101,
            "name":"Christ Joseph",
            "email":"emp101@example.com",
            "attendance": [
                {
                    "last_day_present":"13-05-2021",
                    "days_present":0,
                    "grade":A
                }
            ]
        },
        {
            "emp_id":102,
            "name":"Paolo Jobh",
            "email":"emp102@example.com",
            "attendance": [
                {
                    "last_day_present":"13-05-2021",
                    "days_present":100,
                    "grade":B
                }
            ]
        },
        {
            "emp_id":103,
            "name":"Subo Paul",
            "email":"emp103@example.com",
            "attendance": [
                {
                    "last_day_present":"13-05-2021",
                    "days_present":15,
                    "grade":B
                }
            ]
        },
    ]
}

PHP Script :

$json = json_decode($curl_response, true);

echo "<table border='1' width='50%' align='center'>";
foreach($json['employees'] as $data)
{
   if($json['employees'][0]['attendance']['days_present'] == 0) {
     //exclude the employees whose days_present is 0
   } else { 
   echo "<tr>";
   echo "<td>$json['employees']['emp_id']</td>";
   echo "<td>$json['employees']['email']</td>";
   echo "<td>$json['employees']['days_present']</td>";
   echo "<td>$json['employees'][0]['attendace']['grade']</td>";
   echo "</tr>";
  }
}

Getting Output:

(empty table)

Expecting Output:

<table border="1" width="50%" align="center">
<!-- Display the JSON array response here -->
<tr>
  <td>102</td>
  <td>emp102@example.com</td>
  <td>100</td>
  <td>B</td>
</tr>
<tr>
  <td>103</td>
  <td>emp103@example.com</td>
  <td>15</td>
  <td>B</td>
</tr>

Someone, please help me to figure out what's wrong with the script. Is there any better method available to execute the script faster and with less traffic load?

AmlanRD
  • 41
  • 8

2 Answers2

1

The foreach/as loop you are using puts each element of the $json['employees'] array into a variable called $data. You need to adjust the code like this.

Change

foreach($json['employees'] as $data)
{
   if($json['employees'][0]['attendance']['days_present'] == 0) {
     //exclude the employees whose days_present is 0
   } else { 
   echo "<tr>";
   echo "<td>$json['employees']['emp_id']</td>";
   echo "<td>$json['employees']['email']</td>";
   echo "<td>$json['employees']['days_present']</td>";
   echo "<td>$json['employees'][0]['attendace']['grade']</td>";
   echo "</tr>";
  }
}

to

foreach($json['employees'] as $data)
{
   if($data['attendance'][0]['days_present'] == 0) {
     //exclude the employees whose days_present is 0
   } else { 
   echo "<tr>";
   echo "<td>" . $data['emp_id'] . "</td>";
   echo "<td>" . $data['email'] . "</td>";
   echo "<td>" . $data['attendance'][0]['days_present'] . "</td>";
   echo "<td>" . $data['attendance'][0]['grade'] . "</td>";
   echo "</tr>";
  }
}
Will Walsh
  • 1,799
  • 1
  • 6
  • 15
  • Haha I didn't even get that far, but that is indeed another problem :P I was trying to test some of his code but I couldn't even decode the JSON he supplied, seems to be invalid. – Crimin4L May 13 '21 at 02:04
  • Followed your answer. But the last two columns are showing empty. – AmlanRD May 13 '21 at 02:04
  • I have fixed the code for the last 2 columns, although as @Crimin4L says, I suspect it won't work anyway due to an issue with the JSON. – Will Walsh May 13 '21 at 02:19
  • 1
    I adjusted and approved that edit @Crimin4L. Good thinking on the concatenation rather than interpolation for array variables. I kept the array reference on `$data['attendance']` due to the original poster's 'JSON' showing 'attendance' as an array. – Will Walsh May 13 '21 at 02:36
  • Haha thank you @WillWalsh , and I was wondering why you put them `[0]`'s there but then it clicked and I realized why; Those brackets I noted in my answer was just an unnamed dimension lolol – Crimin4L May 13 '21 at 02:42
0

Very simple problem; I rewrote the entire json array you provided and encoded it to JSON to see the correct structure which should look like:

{
    "employees": [
        {
            "emp_id": 101,
            "name": "Christ Joseph",
            "email": "emp101@example.com",
            "attendance": {
                "last_day_present": "13-05-2021",
                "days_present": 0,
                "grade": "A"
            }
        },
        {
            "emp_id": 102,
            "name": "Paolo Jobh",
            "email": "emp102@example.com",
            "attendance": {
                "last_day_present": "13-05-2021",
                "days_present": 100,
                "grade": "B"
            }
        },
        {
            "emp_id": 103,
            "name": "Subo Paul",
            "email": "emp103@example.com",
            "attendance": {
                "last_day_present": "13-05-2021",
                "days_present": 15,
                "grade": "B"
            }
        }
    ]
}

1st: You don't have quotations around your ["attendance"]["grade"] values. Anything that has non-alphanumeric characters or alphabetic characters you need quotations around. ONLY Numeric values can be left without quotations (correct me if I am wrong, but I am pretty sure this is how it goes)

2nd: Around the ["attendance"] values you have square brackets, which is putting it inside an unnamed dimension that is really unnecessary and just makes retrieving it a bit harder.

3rd: The VERY last comma at the bottom that looks like this: }, is the other reason you are not getting any response from your json_decode. Remove that last pointless comma and do at lease the 1st step also and it should work.

Here is a your JSON array with the errors pointed out:

{
        "employees": [
            {
                "emp_id":101,
                "name":"Christ Joseph",
                "email":"emp101@example.com",
                "attendance": [ # <---------- 2nd Problem 
                    {
                        "last_day_present":"13-05-2021",
                        "days_present":0,
                        "grade":A # <---------- 1st Problem
                    }
                ] # <---------- 2nd Problem
            },
            {
                "emp_id":102,
                "name":"Paolo Jobh",
                "email":"emp102@example.com",
                "attendance": [ # <---------- 2nd Problem
                    {
                        "last_day_present":"13-05-2021",
                        "days_present":100,
                        "grade":B # <---------- 1st Problem
                    }
                ] # <---------- 2nd Problem
            },
            {
                "emp_id":103,
                "name":"Subo Paul",
                "email":"emp103@example.com",
                "attendance": [ # <---------- 2nd Problem
                    {
                        "last_day_present":"13-05-2021",
                        "days_present":15,
                        "grade":B # <---------- 1st Problem
                    }
                ] # <---------- 2nd Problem
            }, # <---------- 3rd Problem
        ]
    }

So basically, your curl response is invalid. I'm not sure if that is something you are in control of but that looks like the problem. I took your whole JSON array and put it into sandbox.onlinephpfunctions.com trying to decode it into an array and print it out (ref.), but it wouldn't work until I did the couple things explained above.

Crimin4L
  • 610
  • 2
  • 8
  • 23
  • 1
    1st - Somewhat correct. PHP will process associative arrays with indexes without quotes around them, but it will throw a warning. It is best practice to include the quotes. – Will Walsh May 13 '21 at 02:17