3
我有一个类说,Foo
有一个名为bar
一个json
字符串属性里面不工作:[PHP Fiddle Link]未设置()类方法
<?php
class Foo {
public $bar = '{"1455260079":"Tracking : #34567808765098767 USPS","1455260723":"Delivered","1455261541":"Received Back"}';
public function getBar(){
return (array) json_decode($this->bar);
}
public function remove($timestamp){
$newBar = $this->getBar();
print_r($newBar);
unset($newBar[$timestamp]);
print_r($newBar);
$this->bar = json_encode($newBar);
}
}
现在,除去从酒吧元素,我做以下,我想不通为什么它不删除:
$foo = new Foo();
$foo->remove("1455261541");
echo $foo->bar;
打印出:
Array
(
[1455260079] => Tracking : #34567808765098767 USPS
[1455260723] => Delivered
[1455261541] => Received Back
)
Array
(
[1455260079] => Tracking : #34567808765098767 USPS
[1455260723] => Delivered
[1455261541] => Received Back
)
{"1455260079":"Tracking : #34567808765098767 USPS","1455260723":"Delivered","1455261541":"Received Back"}
背后的原因是什么?任何帮助?
嗯,这作品很酷!这很奇怪,我们不能用钥匙来解除一个该死的阵列!谢谢。 – tika
使用'(array)'进行数组类型转换会将键转换为字符串请参阅原始代码中的var_dump数组 –