I am trying to json_encode an array which his var_dump gives:

   array(3) {
      [1]=>
      array(3) {
        ["comment_id"]=>
        string(1) "1"
        ["erasable"]=>
        string(1) "1"
        ["comment"]=>
        string(6) "test 1"
      }
      [2]=>
      array(3) {
        ["comment_id"]=>
        string(1) "2"
        ["erasable"]=>
        string(1) "1"
        ["comment"]=>
        string(6) "test 1"
      }
      [3]=>
      array(3) {
        ["comment_id"]=>
        string(1) "3"
        ["erasable"]=>
        string(1) "1"
        ["comment"]=>
        string(6) "jhghjg"
      }
    }

The encoded string looks:

 {"1":{"comment_id":"1","erasable":"1","comment":"test 1"},
     "2":{"comment_id":"2","erasable":"1","comment":"test 1"},
     "3":{"comment_id":"3","erasable":"1","comment":"jhghjg"}}

While I need it as:

 [{"comment_id":"1","erasable":"1","comment":"test 1"},
    {"comment_id":"2","erasable":"1","comment":"test 1"},
    {"comment_id":"3","erasable":"1","comment":"jhghjg"}]

As the php.ini/json__encode documentation says it should.
Any ideas?

    Your array is clearly being rendered in the fashion you are showing above. You have an associative array with numbers as keys (1-3) with sub elements. Now, if you restructure your array like this, I seem to get what you were looking for:

    $array = array(
    	array("comment_id"=>"1","erasable"=>"1","comment"=>"test 1"), 
    	array("comment_id"=>"2","erasable"=>"1","comment"=>"test 1"),
    	array("comment_id"=>"3","erasable"=>"1","comment"=>"jhghjg")
    );

    Now we have a non-associative array with associative arrays within.

    print_r($array);

    renders:

    Array
    (
        [0] => Array
            (
                [comment_id] => 1
                [erasable] => 1
                [comment] => test 1
            )
    
    [1] => Array
        (
            [comment_id] => 2
            [erasable] => 1
            [comment] => test 1
        )
    
    [2] => Array
        (
            [comment_id] => 3
            [erasable] => 1
            [comment] => jhghjg
        )
    
    )
    
    echo json_encode($array);

    renders:

    [{"comment_id":"1","erasable":"1","comment":"test 1"},
    {"comment_id":"2","erasable":"1","comment":"test 1"},
    {"comment_id":"3","erasable":"1","comment":"jhghjg"}]
      9 years later

      I'm having an almost similar problem where json_encode is all of a sudden ignoring the arrays. I built the arrays in php and passed it to the JavaScript variables:

      <?php /*?>
      		$id[] = $row['id'];
      		$book[] = $row['book'];		
      		$bookTitle[] = $row['book_title'];
      		$recordType[] = $row['recordType'];		
      		$chapter[] = $row['chapter'];
      		$verse[] = $row['verse'];
      		$bookSpoke[] = $row['book_spoke']; 
      		$chapterSpoke[] = $row['chapter_spoke']; 
      		$verseSpoke[] = $row['verse_spoke'];
      		$textData[] = $row['text_data'];		
      		$strongs[] = $row['strongs'];
      		$transliteration[] = $row['transliteration'];
      		$etymology[] = $row['etymology'];
      		$etymStrongs[] = $row['etym_strongs'];
      		$etymDesc[] = lcfirst($row['etym_desc']);
      		$hebWord[] = hebrevc($row['heb_word']);
      		$added[] = $row['added'];
      		$also[] = $row['also'];
      <?php */?>	
      
      var js_text1 = <?php echo json_encode($js_text1); ?>;
      
      var js_text2 = <?php echo json_encode($js_text2); ?>;
      
      <?php /*?> 
      	0	$i, 
      	1	$newStrongsResult[$i], 
      	2	$joinedTextData, 
      	3	count($searchStrongs_textData[$i]), 
      	4	$joined_Transliteration_Hebrewword_Strongs_etymDesc, 
      	5	$searchStrongs_added[0], 
      	6	$searchStrongs_also[0], 
      	7	$colors[$i]
      <?php */?>
      
      var js_data = <?php echo json_encode($js_data); ?>;
      
      var js_newStrongsResult = [<?php echo '"'.implode('","', $newStrongsResult).'"' ?>]; //$php_array
      
        Write a Reply...