Why json_encode( all of a sudden stopped encoding the php arrays

I’m stuck on why json_encode( all of a sudden stopped encoding the php arrays int JavaScript arrays. It shows them empty.

    $js_data = Array();

    for($i=0;$i<count($newStrongsResult);$i++){
        
        $searchStrongs = "SELECT * FROM ".$dbTable13." WHERE strongs = '".$newStrongsResult[$i]."' AND ";
        $searchStrongs .= "(";
        $searchStrongs .= " (book = '".$getB1."' AND chapter = '".$getC1."') OR ";
        $searchStrongs .= " (book = '".$getB2."' AND chapter = '".$getC2."') ";
        $searchStrongs .= ")";
        $searchStrongsResult = $conn->query($searchStrongs);
        
        while($row = $searchStrongsResult->fetch(PDO::FETCH_ASSOC)) {
            $searchStrongs_id[$i][] = $row['id'];
            $searchStrongs_book[$i][] = $row['book'];        
            $searchStrongs_bookTitle[$i][] = $row['book_title'];
            $searchStrongs_recordType[$i][] = $row['recordType'];        
            $searchStrongs_chapter[$i][] = $row['chapter'];
            $searchStrongs_verse[$i][] = $row['verse'];
            $searchStrongs_bookSpoke[$i][] = $row['book_spoke']; 
            $searchStrongs_chapterSpoke[$i][] = $row['chapter_spoke']; 
            $searchStrongs_verseSpoke[$i][] = $row['verse_spoke'];
            $searchStrongs_textData[$i][] = $row['text_data'];        
            $searchStrongs_strongs[$i][] = $row['strongs'];
            $searchStrongs_transliteration[$i][] = $row['transliteration'];
            $searchStrongs_etymology[$i][] = $row['etymology'];
            $searchStrongs_etymStrongs[$i][] = $row['etym_strongs'];
            $searchStrongs_etymDesc[$i][] = lcfirst($row['etym_desc']);
            $searchStrongs_hebWord[$i][] = hebrevc($row['heb_word']);
            $searchStrongs_added[$i][] = $row['added'];
            $searchStrongs_also[$i][] = $row['also'];
        }
        
        $joinedTextData = "('".implode("', \n'", $searchStrongs_textData[$i])."')\n\n";
        
        $joinedTransliteration = "('".implode("', \n'", $searchStrongs_transliteration[$i])."')\n\n";
        $joinedHebrewword = "('".implode("', \n'", $searchStrongs_hebWord[$i])."')\n\n";
        $joinedStrongs_etymDesc = "('".implode("', '", $searchStrongs_etymDesc[$i])."')\n\n";
        
        $joined_Transliteration_Hebrewword_Strongs_etymDesc = Array();
        
        for($j=0;$j<count($searchStrongs_transliteration);$j++){
            $joined_Transliteration_Hebrewword_Strongs_etymDesc = Array(    
                $searchStrongs_transliteration[$j][0],
                $searchStrongs_hebWord[$j][0],
                $searchStrongs_etymDesc[$j][0]);
        }

        $js_data[] = Array($i, $newStrongsResult[$i], $joinedTextData, count($searchStrongs_textData[$i]), $joined_Transliteration_Hebrewword_Strongs_etymDesc, $searchStrongs_added[0], $searchStrongs_also[0], $colors[$i]);
    }

/**************************************/
<?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

Where do you assign values to $js_text1 and $js_text2 in your PHP?

I discovered why js_encode wasn’t working. It was because of the Hebrew columns in the db table. I had defined the charset:

header("Content-Type: text/html; charset=utf-8");
define("DSN", "mysql:host=localhost; dbname=".$db."; charset=utf8");
define("USERNAME", "root");
define("PASSWORD", "");

But even though these were defined it still didn’t work.

Then I opened xampp/mysql/bin/my.ini and changed:

#collation_server=utf8_unicode_ci
#character_set_server=utf8

to

#character-set-server  = utf8mb4
#collation-server      = utf8mb4_general_ci

It works (now due to the big info the browser keeps crashing). I wonder if there was an easier way instead of changing my.ini?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.