1

I'm experiencing a problem when trying to json_encode() a non-associative array in PHP:

echo json_encode(array(array(1,1), array(1,1)), JSON_FORCE_OBJECT) ;
// Non-associative array output as object: {"0":{"0":1,"1":1},"1":{"0":1,"1":1}} 

echo json_encode(array(array(1,1), array(1,1))); 
// Non-associative array output as array: "" (empty string)

The PHP version used on my server is 5.3.

How do I get a non-associative array output as array [[1,1],[1,1]]?

10
  • I can get it to work using your second code. It outputs "[[1,1],[1,1]]" Commented Sep 15, 2014 at 16:48
  • Should work as intended 3v4l.org/pknk5 Commented Sep 15, 2014 at 16:49
  • @MichaelBerkowski You forgot the PHP tags. However, this should work. Commented Sep 15, 2014 at 16:51
  • @MCEmperor wrong link copied. I changed it to 3v4l.org anyway... Commented Sep 15, 2014 at 16:52
  • there's no way json_encode would convert a non-empty array to an empty string. I'm calling PEBKAC. Even an empty array would encode as [], not "". Commented Sep 15, 2014 at 16:56

1 Answer 1

1

Problem solved after PHP version upgraded to >=5.5

Sign up to request clarification or add additional context in comments.

Comments

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.