Php json_encode返回长度不为零的空字符串
为什么json_encodePhp json_encode返回长度不为零的空字符串,php,json,string,Php,Json,String,为什么json_encode(在SimpleArtItem::getJSONPriceRules()中)返回奇怪的字符串?字符串为空,但长度不是零(strlen($res)>0)和var\u dump打印*字符串“”(长度=35)*) 类simpleartitem{ 公共资源ID美元; 公有产权; 公共网址; 公费$daysunt; 公众$dateFrom; 公帑$dateTo; 公共$类型; /** * [ *[price,minDays,maxDays], * [..], * .. *
(在SimpleArtItem::getJSONPriceRules()中)返回奇怪的字符串?字符串为空,但长度不是零<代码>(strlen($res)>0)
和var\u dump
打印*字符串“”(长度=35)*)
类simpleartitem{
公共资源ID美元;
公有产权;
公共网址;
公费$daysunt;
公众$dateFrom;
公帑$dateTo;
公共$类型;
/**
* [
*[price,minDays,maxDays],
* [..],
* ..
* ]
*maxDays是可选参数!
*@var数组
*/
公共价格规则;
函数构造($resourceId){
$this->resourceId=$resourceId;
$this->lazyParamsLoader();
}
函数lazyParamsLoader(){
全球$modx;
$resource=$modx->getObject('modResource',['id'=>$this->resourceId]);
$props=$resource->toArray();
$this->title=$props['pagetitle'];
$this->photoURL=$resource->getTVValue('equip-photo');
$one=$resource->getTVValue('Equipm-day-price');
$two=$resource->getTVValue('equipment-two-days-price');
$five=$resource->getTVValue('equipment-five-days-price');
$this->priceRules=[
[(int)$1,1,1],
[(int)$2,2,4],
[(int)$5,5],
];
}
函数getJSONPriceRules(){
//var_dump(json_encode($this->priceRules),$this->priceRules);
$res=json_encode($this->priceRules);
var_dump($res);//我想到了这可能也很有用。零宽度空间?$this->priceRules=[[(int)$one,1,1],(int)$two,2,4],(int)$five,5],];json_encode($this->priceRules);
为什么?你能编辑你的问题并发布var_dump($this->priceRules)的输出吗;
?另外,您的PHP版本是什么?
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;
}