4

This is my array.

http://pastebin.com/eTrJ2PVB

When I return this array like this, I get an error.

return \Response::json($response, 200, [], JSON_NUMERIC_CHECK);

However, when I remove JSON_NUMERIC_CHECK, I get the response; but the problem is I need numeric values.

What could be done to resolve this?

nirvair
  • 4,001
  • 10
  • 51
  • 85
  • Well.. an array is not a numeric. – mrun Mar 12 '16 at 11:42
  • @mrun I need the values to be numeric. – nirvair Mar 12 '16 at 11:53
  • In that case I'd suggest validating the array by yourself and not relying on `json_encode` on that. Its purpose is not to validate. – mrun Mar 12 '16 at 11:59
  • But why does JSON_NUMERIC_CHECK give this error? – nirvair Mar 12 '16 at 12:43
  • Why do you need them to be numeric? How do you use that array? – Davor Minchorov Mar 13 '16 at 10:10
  • my guess is there is something wrong with your array. have you seen [this question](http://stackoverflow.com/questions/26022390/the-response-content-must-be-a-string-or-object-implementing-tostring-boo?rq=1) – tam5 Mar 13 '16 at 12:15
  • The function works fine. It is only giving error for this array. Yes, I have seen that question and did all the things that were mentioned in answers. – nirvair Mar 14 '16 at 08:55
  • i ran the function with the array you posted and I was not able to recreate your error. can you verify that `return \Response::json(['number' => '16'], 200, [], JSON_NUMERIC_CHECK);` runs correctly? – tam5 Mar 14 '16 at 19:02
  • Please share more details, like the code involved and your attempts to resolve the problem. Please do **not** post the code anywhere else – Nico Haase Dec 03 '21 at 11:03

1 Answers1

1

You don't mention what error you are getting, but I can't reproduce any errors either. In fact this code at http://pastebin.com/GJWrCgwN shows your array returned as a perfectly formatted JSON with literal numbers instead of strings - see results at http://pastebin.com/cYDzDGyE.

BTW, you can also use this alternative syntax where you don't need to mess with HTTP response status code or headers, just the JSON encoding options:

return response()->json($response)->setEncodingOptions(JSON_NUMERIC_CHECK);

Check it out at Symfony\Component\HttpFoundation\JsonResponse.

Malta
  • 544
  • 3
  • 8
  • Could you please elaborate more your answer adding a little more description about the solution you provide? – abarisone Apr 15 '16 at 15:18
  • @abarisone, I elaborated my answer as you suggested. I don't see a real problem in the original question, but wanted to recommend a better solution anyway. Thank you. – Malta Apr 16 '16 at 00:36