0

I have a php array which i json_encode to use in javascript but i need one of the to be in quotes. I do not know how to go about it. Here is my php array

Array
(
[0] => Array
    (
        [label] => 03
        [value] => 2
    )

[1] => Array
    (
        [label] => 05
        [value] => 2
    )

[2] => Array
    (
        [label] => 06
        [value] => 12
    )

[3] => Array
    (
        [label] => 07
        [value] => 12
    )

[4] => Array
    (
        [label] => 08
        [value] => 1
    )

)

heres my json_encode

[{"label":"03","value":2},
{"label":"05","value":2},
{"label":"06","value":12},
{"label":"07","value":12},
{"label":"08","value":1}]

what i want is

[{"label":"03","value":"2"},
{"label":"05","value":"2"},
{"label":"06","value":"12"},
{"label":"07","value":"12"},
{"label":"08","value":"1"}]
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
user7498826
  • 173
  • 3
  • 9
  • 1
    Why do you need the value in quotes? If you require `string` data type just cast it within the PHP. – Scott Aug 23 '17 at 08:46
  • 3
    Please have in mind that if you quote the numbers the data type will be interpreded differently. a "2" is a string and 2 is a number. You can try to explicit cast the number to a string in php. – wartoshika Aug 23 '17 at 08:46
  • json_encode($strings, JSON_NUMERIC_CHECK) – Barclick Flores Velasquez Aug 23 '17 at 08:49
  • 1
    Possible duplicate of [How can I force PHP's json\_encode integer values to be encoded as String?](https://stackoverflow.com/questions/33706043/how-can-i-force-phps-json-encode-integer-values-to-be-encoded-as-string) – Arun Kumaresh Aug 23 '17 at 09:00

2 Answers2

1

Use foor loop to iterate through the array of arrays and convert every int to string.

for (int i = 0; i < firstArray.length; i++){
for (int j = 0; j < firstArray[i].length; j++){
    firstArray[i][j][1] = firstArray[i][j][1].toString();
}}

This could work for you.

And if you strictly know that you will have json object with 2 items in every item in array you could use:

for (int i = 0; i < firstArray.length; i++){ firstArray[i][0][1] = firstArray[i][0][1].toString(); firstArray[i][1][1] = firstArray[i][1][1].toString(); }

alxbxbx
  • 313
  • 1
  • 5
  • 18
0

In your php array you have:

[1] => Array
    (
        [label] => 05 (as string)
        [value] => 2 (as integer)
    )

json_encode checks the type of value that you have in this array. According to the json standard integer can be stored without quotes.

You can:

  • convert integers to strings (before json_encode),

  • or use array_map function in php (before json_encode)

something like this:

$data = json_encode(array_map('strval', $data)); // all variables are now strings
kGielo
  • 371
  • 1
  • 9
  • using array_map converts my array to the below Array ( [0] => {"label":"03","value":"2"} [1] => {"label":"05","value":"2"} [2] => {"label":"06","value":"12"} [3] => {"label":"07","value":"12"} [4] => {"label":"08","value":"1"} ) how can i convert this back to a normal array like this [1] => Array ( [label] => 05 [value] => 2 ) – user7498826 Aug 23 '17 at 11:14
  • @user7498826 you need convert php array to array without quotes? or json array to php array without quotes? – kGielo Aug 23 '17 at 11:18
  • basically i need to convert the array so that when i json ecode it this time i will get a json data with quotes on values..because currently when i encode it just takes away the quotes – user7498826 Aug 23 '17 at 11:37