I have the following php code. I am able to get the value stored in my variable and printed. But after json_encode, the value became 'empty'. Is there a bug in json_encode ? I am using Php 7.2 on ubuntu 18
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://www.thestar.com.my/rss/news/nation');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xml = curl_exec($ch);
curl_close($ch);
$rss = simplexml_load_string($xml);
$cnt = count($rss->channel->item);
$items = array();
$items[0]['title'] = $rss->channel->item[0]->title;
$items[0]['url'] = $rss->channel->item[0]->link;
echo $items[0]['title'] . PHP_EOL;
echo $items[0]['url'] . PHP_EOL;
echo json_encode($items) . PHP_EOL;
?>
See the output below. What is wrong with the json_encode ? Why after the json_encode, the title became empty ?
Sabah updates SOPs for places of worship and childcare centres
https://www.thestar.com.my/news/nation/2021/06/13/sabah-updates-sops-for-places-of-worship-and-childcare-centres
[{"title":{"0":{}},"url":{"0":"https:\/\/www.thestar.com.my\/news\/nation\/2021\/06\/13\/sabah-updates-sops-for-places-of-worship-and-childcare-centres"}}]