nick
nick

Reputation: 333

PHP Json response formatting

I have the following script that returns a json response.

$response["customer_creds"] =array(array('customer_names'=>$user['name'], 'customer_email' => $user['email'], 'customer_id' => $user['customer_id'], 'customer_type' => $user['customer_type'], 'rating' => $user['rating']));

The above script returns:

"customer_creds": [
    {
      "customer_names": "John Doe",
      "customer_email": "[email protected]",
      "customer_id": "123456",
      "customer_type": "1",
      "rating": "4"
    }
  ],

Now I want my json to return the customer_type as an object.("customer_type": [1],

I have tried json decoding and encoding on the same script but nothing seems to work. Any workarounds on this? At a later stage I'll want to have my json to return multiple customer types. The final response should be something like this:

"customer_creds": [
  {
      "customer_names": "John Doe",
      "customer_email": "[email protected]",
      "customer_id": "123456",
      "customer_type": [1,2,3],
      "rating": "4"
    }
  ],

Any suggestion would be highly appreciated. Thanks

Upvotes: 0

Views: 44

Answers (1)

Here2Help
Here2Help

Reputation: 484

You just want the customer_type to be an array of values, instead of just one value?

$response["customer_creds"] = array(
    array(
        'customer_names' => $user['name'], 
        'customer_email' => $user['email'], 
        'customer_id' => $user['customer_id'], 
        'customer_type' => array($user['customer_type']), // Just wrap it with array()
        'rating' => $user['rating']
    )
);

Upvotes: 2

Related Questions