-2

I am trying to extract and print a list of customers from the following json response.

Unfortunately it returns NULL

$customers_response = '{
                            "_embedded": {
                                "customers": [
                                    {
                                        "id": "1",
                                        "firstName": "John",
                                        "lastName": "Doe",
                                        "email": "johndoe@nomail.net",
                                        "type": "personal",
                                        "status": "verified",
                                        "created": "2021-06-06T05:00:09.923Z",
                                        "address1": "99-99 33rd St",
                                        "city": "Some City",
                                        "state": "NY",
                                        "postalCode": "11101"
                                    },
                                    {
                                        "id": "2",
                                        "firstName": "Jane",
                                        "lastName": "Doe",
                                        "email": "janedoe@nomail.net",
                                        "type": "personal",
                                        "status": "verified",
                                        "created": "2021-06-06T05:00:09.923Z",
                                        "address1": "99-99 33rd St",
                                        "city": "Some City",
                                        "state": "NY",
                                        "postalCode": "11101"
                                    }
                                ]
                            },
                            "total": 1
                        }';


$customer_list = json_decode($customers_response)->{"_embedded"};
$customer_list = json_encode(json_decode($customer_list, true));

foreach ($customer_list["customers"] as $customer) {
    $all_customers .= $customer{"id"}.": ".$customer{"firstName"}." ".$customer{"lastName"}."<br/>";
}

echo $all_customers;

I have tried the above code but it seems not to works. What do I need to change or is there any easier workaround to extract the list?

Eduu
  • 3
  • 2
  • When you run `json_encode` it returns a string. But you are trying to iterate the string `$customer_list` as if it was an array. – Chin. Udara Jun 06 '21 at 06:42
  • Enabling error_reporting and displaying errors will help a lot. [How do I get PHP errors to display?](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display?rq=1) – brombeer Jun 06 '21 at 06:43

1 Answers1

0

Your program is not very efficient and also the logic is flawed as I have mentioned in my comment.

You can try the following; also follow the comments to understand the code.

//$customer_list = json_decode($customers_response)->{"_embedded"}; // you are decoding the JSON here; which return a JSON object
//$customer_list = json_encode(json_decode($customer_list, true)); // you are again encoding the json object to a JSON string
$customer_list = json_decode($customers_response, true); // returns an associative array after parsing the json string.
$all_customers = ""; // define the variable if its not already defined. 
foreach ($customer_list["_embedded"]["customers"] as $customer) {
    $all_customers .= $customer["id"].": ".$customer["firstName"]." ".$customer["lastName"]."<br/>";
}

echo $all_customers;
Chin. Udara
  • 694
  • 4
  • 19
  • Depending on the version this will throw "_Fatal error: Array and string offset access syntax with curly braces is no longer supported in ..._" – brombeer Jun 06 '21 at 06:48
  • And a "_Notice: Undefined variable: all_customers in ..._" – brombeer Jun 06 '21 at 06:49
  • Thanks for pointing the curly brace issue; I am assuming the `$all_customers` is already defined based on OPs code. – Chin. Udara Jun 06 '21 at 06:50