json_encode返回空字符串而不是零长度

时间:2015-05-05 14:37:21

标签: php json string

为什么json_encode (in SimpleCartItem::getJSONPriceRules() )会返回奇怪的字符串?字符串为空,但长度不为零。 (strlen($res) > 0)var_dump打印*string '' (length=35)*)

class SimpleCartItem {
    public $resourceId;
    public $title;
    public $photoURL;
    public $daysCount;
    public $dateFrom;
    public $dateTo;
    public $type;

    /**
     * [
     *  [price, minDays, maxDays],
     *  [..],
     *  ..
     * ]
     * maxDays is optional param!
     * @var Array
     */
    public $priceRules;

    function __construct($resourceId) {
        $this->resourceId = $resourceId;
        $this->lazyParamsLoader();
    }

    function lazyParamsLoader() {
        global $modx;
        $resource = $modx->getObject('modResource', ['id' => $this->resourceId] );
        $props = $resource->toArray();

        $this->title = $props['pagetitle'];
        $this->photoURL = $resource->getTVValue('equip-photo');

        $one = $resource->getTVValue('equip-day-price');
        $two = $resource->getTVValue('equip-two-days-price');
        $five = $resource->getTVValue('equip-five-days-price');

        $this->priceRules = [
            [(int)$one, 1, 1],
            [(int)$two, 2, 4],
            [(int)$five, 5],
        ];
    }

    function getJSONPriceRules() {
        //var_dump(json_encode($this->priceRules), $this->priceRules);
        $res = json_encode($this->priceRules);

        var_dump($res); // <--- string '' (length=35)

        for ($i=0; $i<strlen($res);$i++)
            echo $res[$i]; // <--- empty!

        $res .= 'h'; 
        var_dump($res); // <--- string 'h' (length=36)
        return $res;
}

0 个答案:

没有答案
相关问题