Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP json_encode array(3) return false

I have a mysqli query that returns a multidimensional array.

I'm trying to encode the php array:

array(3) {
  [0]=>
  array(8) {
    ["cod_evento"]=>
    string(1) "3"
    ["titulo"]=>
    string(20) "Fiesta de cumpleaños"
    ["descripcion"]=>
    string(23) "Mi fiesta de cumpleaños"
    ["puntuacion"]=>
    string(1) "1"
    ["direccion"]=>
    string(13) "c/ falsa 1234"
    ["imagen"]=>
    string(115) "http://static.guim.co.uk/sys-images/Guardian/Pix/pictures/2014/4/11/1397210130748/Spring-Lamb.-Image-shot-2-011.jpg"
    ["fecha"]=>
    string(19) "2015-01-01 00:00:00"
    ["cod_usuario"]=>
    string(1) "1"
  }

  [....]
}

But when I try json_encode($array) it returns bool(false)

When I try to encode an array(2) or (1), then it works.

like image 528
javier6_almansa Avatar asked Oct 21 '25 10:10

javier6_almansa


2 Answers

The error happened because your array had a non-unicode character.

like image 106
SmallChess Avatar answered Oct 24 '25 02:10

SmallChess


From the docs

http://php.net/manual/en/function.json-encode.php

Returns a JSON encoded string on success or FALSE on failure.

to see the reason of failure

http://php.net/manual/en/function.json-last-error-msg.php

To answer the question in the comments

json_encode produce JSON_ERROR_UTF8 from MSSQL-SELECT

like image 24
exussum Avatar answered Oct 24 '25 02:10

exussum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!