I am a .NET guy with fairly long experience but recently pulled into a PHP project so need some help. I have this PHP code which prepares a final song object and then JSONifies it with json_encode()
but I strangly I am seeing some extra value null
added at the end of JSON string and I dont seem to understand why is that.
Here is my PHP code
foreach($result as $item) {
++$index;
$hymn->id=$item['id'];
$hymn->refrain=$item['refrain'];
if ($index == 1) { $hymn->stanza1=$item['stanzaText']; }
if ($index == 2) { $hymn->stanza2=$item['stanzaText']; }
if ($index == 3) { $hymn->stanza3=$item['stanzaText']; }
if ($index == 4) { $hymn->stanza4=$item['stanzaText']; }
if ($index == 5) { $hymn->stanza5=$item['stanzaText']; }
if ($index == 6) { $hymn->stanza6=$item['stanzaText']; }
if ($index == 7) { $hymn->stanza7=$item['stanzaText']; }
if ($index == 8) { $hymn->stanza8=$item['stanzaText']; }
if ($index == 9) { $hymn->stanza9=$item['stanzaText']; }
if ($index == 10) { $hymn->stanza10=$item['stanzaText']; }
}
json_encode($hymn);
Above code produces a JSON with extra null
after the ending curly braces as shown below.
{
"id":"1",
"refrain" : "Jesus, Jesus, how I trust Him!
How I\u2019ve proved Him o\u2019er and o\u2019er;
Jesus, Jesus, precious Jesus!
Oh, for grace to trust Him more!",
"stanza1" : "Tis so sweet to trust in Jesus,
Just to take Him at His Word;
Just to rest upon His promise,
And to know, \u201cThus saith the Lord!\u201d",
"stanza2" : "Oh, how sweet to trust in Jesus,
Just to trust His cleansing blood;
And in simple faith to plunge me
\u2019Neath the healing, cleansing flood!",
"stanza3" : "Yes, \u2019tis sweet to trust in Jesus,
Just from sin and self to cease;
Just from Jesus simply taking
Life and rest, and joy and peace.",
"stanza4" : "I\u2019m so glad I learned to trust Thee,
Precious Jesus, Savior, Friend;
And I know that Thou art with me,
Wilt be with me to the end"
}null
Kindly help me to understand what wrong am I doing here?