0

This data is fetched from CURL in php and after decode I got this result, could you help me to print it in a table form so that I will be able to show it in web page

    <?php
                $user_curl = curl_init();

                curl_setopt_array($user_curl, array(
                    CURLOPT_URL => "https://gorest.co.in/public-api/users",
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_ENCODING => "",
                    CURLOPT_MAXREDIRS => 10,
                    CURLOPT_TIMEOUT => 60,
                    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                    CURLOPT_CUSTOMREQUEST => "GET",
                    CURLOPT_POSTFIELDS => "",
                    CURLOPT_HTTPHEADER => array(
                        "cache-control: no-cache",
                        "content-type: application/json",
                    ),
                ));


                $user_curl_response = curl_exec($user_curl);
                $user_curl_err = curl_error($user_curl);

                if ($user_curl_response) {

                $result = json_decode($user_curl_response,true);

                 echo "<pre>";
                 print_r($result);

                    
                } else {
                    echo 'No data found';
                }
Array
(
    [code] => 200
    [meta] => Array
        (
            [pagination] => Array
                (
                    [total] => 4018
                    [pages] => 402
                    [page] => 1
                    [limit] => 10
                )

        )

    [data] => Array
        (
            [0] => Array
                (
                    [id] => 4092
                    [name] => Dhanu Nambeesan
                    [email] => [email protected]
                    [gender] => male
                    [status] => inactive
                )

            [1] => Array
                (
                    [id] => 4091
                    [name] => Sen. Ekadant Reddy
                    [email] => [email protected]
                    [gender] => male
                    [status] => inactive
                )

            [2] => Array
                (
                    [id] => 4089
                    [name] => Narinder Acharya III
                    [email] => [email protected]
                    [gender] => female
                    [status] => inactive
                )

            [3] => Array
                (
                    [id] => 4088
                    [name] => Fr. Agastya Saini
                    [email] => [email protected]
                    [gender] => male
                    [status] => inactive
                )

            [4] => Array
                (
                    [id] => 4087
                    [name] => Bhuvaneshwar Dwivedi
                    [email] => [email protected]
                    [gender] => male
                    [status] => inactive
                )

            [5] => Array
                (
                    [id] => 4086
                    [name] => Gov. Gorakhanatha Varma
                    [email] => [email protected]
                    [gender] => female
                    [status] => inactive
                )

            [6] => Array
                (
                    [id] => 4085
                    [name] => Kali Bhat MD
                    [email] => [email protected]
                    [gender] => female
                    [status] => inactive
                )

            [7] => Array
                (
                    [id] => 4083
                    [name] => Uttam Gupta
                    [email] => [email protected]
                    [gender] => female
                    [status] => active
                )

            [8] => Array
                (
                    [id] => 4082
                    [name] => Krishna Arora
                    [email] => [email protected]
                    [gender] => male
                    [status] => active
                )

            [9] => Array
                (
                    [id] => 4081
                    [name] => Gov. Aruna Abbott
                    [email] => [email protected]
                    [gender] => female
                    [status] => active
                )

        )

)
ADyson
  • 57,178
  • 14
  • 51
  • 63
  • Why, where are you stuck exactly? Have you tried anything? What's blocking you from making progress? We don't know what you want help with, specifically. We don't just write it all for you on this site. – ADyson Aug 31 '22 at 14:31
  • This data fetched from a api and I want to show it in table form I am trying to put it like this $result['id'] but getting errors I tried loops as well but still getting the error – Ajay Tiwari Aug 31 '22 at 14:36
  • You need to show us the proper code and explain exactly what the error is, we cannot guess your precise issue. And also describe exactly what output you expect. You can [edit] your question to include more info and code. At a guess though, [How can I access an array/object?](https://stackoverflow.com/questions/30680938/how-can-i-access-an-array-object) might be something you need to read, and absorb the principles so you can apply them to your scenario. For more general guidance on your question, see [ask] and how to create a [mre] of your issue. – ADyson Aug 31 '22 at 14:39
  • [How to extract and access data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-to-extract-and-access-data-from-json-with-php) has pretty good explanations regarding accessing the data structure, too. – ADyson Aug 31 '22 at 14:41

2 Answers2

0

This is the basic idea.

$headers = array_keys($data['data'][0]) ?? null;

$headerHtml = "<tr>";
foreach($headers as $header) {
    $headerHtml.= "<th><strong>{$header}<strong></th>";
}
$headerHtml.= "</tr>";


$tableHtml = "";
foreach($data['data'] as $values) {
    $tableHtml.= "<tr>";
    foreach($values as $value) {
        $tableHtml.= "<td>{$value}</td>";
    }
    $tableHtml.= "</tr>";
}

echo "<div>
    <table>
        {$headerHtml}
        {$tableHtml}
    </table
</div>";
Jed Lynch
  • 1,998
  • 18
  • 14
0

From what I understand , you want to be able to get it like this: $result['id'], in that case you need to sort your data , and use the following foreach to get you to 'id':

         foreach($result["data"] as $results){
            echo "<pre>".$results["id"]."</pre>";
         }
AK.Aissat
  • 94
  • 3