0

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

1
  • 3
    If you want customer_type' to be an array, then assign an array: 'customer_type' => array(...). This has nothing to do with JSON. Commented Mar 15, 2016 at 18:23

1 Answer 1

2

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']
    )
);
Sign up to request clarification or add additional context in comments.

5 Comments

it returns "customer_type": [ "1" ], and i want "customer_type": [ 1 ],
@nick isn't that what you wanted?
@nick oh you want an integer 1 instead of a string "1"? Just put intval() around the $user['customer_type'].
a problem is actuakky coming up when i return an array of values. It just returns the first value when i use 'customer_type' => array(intval($user['customer_type']).','.intval($user['customer_driver'])),
@nick: You need to learn how arrays work: secure.php.net/manual/en/language.types.array.php. You are trying to perform string concatenation. An array is not a string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.