0

Why is json_encode (in SimpleCartItem::getJSONPriceRules() ) returning strange string? The string is empty, but length isn't zero. (strlen($res) > 0) and var_dump print *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;
}
Smern
  • 18,746
  • 21
  • 72
  • 90
  • http://en.wikipedia.org/wiki/Zero-width_space comes to mind – MonkeyZeus May 05 '15 at 14:43
  • This could be useful as well. http://stackoverflow.com/questions/9361303/can-i-get-the-unicode-value-of-a-character-or-vise-versa-with-php – MonkeyZeus May 05 '15 at 14:45
  • Zero-width space? `$this->priceRules = [ [(int)$one, 1, 1], [(int)$two, 2, 4], [(int)$five, 5], ]; json_encode($this->priceRules);` Why? – user3213855 May 06 '15 at 04:49
  • Could you edit your question and post the output from a `var_dump($this->priceRules);`? Also, what is your PHP version? – MonkeyZeus May 06 '15 at 13:05

0 Answers0