Php 数组值不变
我想这是因为Php 数组值不变,php,arrays,Php,Arrays,我想这是因为$this和$this->products[$key]中的对象实现了ArrayAccess。但是,任何地方都没有神奇的\uu get或\uu set var_dump($this->products[$key]['selected_options'][$option_key]); // Output: string(7) "Größe:S" $this->products[$key]['selected_options'][$option_key] = "test";
$this
和$this->products[$key]
中的对象实现了ArrayAccess。但是,任何地方都没有神奇的\uu get
或\uu set
var_dump($this->products[$key]['selected_options'][$option_key]);
// Output: string(7) "Größe:S"
$this->products[$key]['selected_options'][$option_key] = "test";
var_dump($this->products[$key]['selected_options'][$option_key]);
// Output: string(7) "Größe:S"
有人知道这里出了什么问题吗
还请注意,这确实有效:
$this->products[$key]['selected_options'] = array($option_key => "test");
// Output: string(4) "test"
阵列访问与$this
(购物车)相同但使用$Products
而不是$data
的产品:
class Product implements ArrayAccess
{
protected $data;
/* **** ArrayAccess **** */
public function offsetExists($offset) {
return isset($this->data[$offset]);
}
public function offsetGet($offset) {
return $this->data[$offset];
}
public function offsetSet($offset , $value) {
$this->data[$offset] = $value;
}
public function offsetUnset($offset) {
unset($this->data[$offset]);
}
}
我运行了这个:
// INPUT: string(7) "Größe:S"
$products = array();
$key = 1;
$option_key = 1;
$products[$key]['selected_options'][$option_key] = "badgers";
$products[$key]['selected_options'][$option_key] = "xxx";
var_dump($products[$key]['selected_options'][$option_key]);
结果是:
字符串(3)“xxx”
所以我认为需要更多的代码?您可能正在尝试更改
如何定义
$this->products
?它的能见度如何?您需要查看当前类的范围,并查看实例化后是否可以覆盖属性。您需要在offsetGet
中通过引用返回
:
直接修改触发调用ArrayAccess::offsetSet()
,而间接修改触发调用ArrayAccess::offsetGet()
。在这种情况下,ArrayAccess::offsetGet()
的实现必须能够通过引用返回,否则将引发一条E_通知
消息
但是,请注意,这仅适用于PHP>=5.3.4是否要告诉我们$value是多少?@Paul
输出:string(7)“Gröe:L”
:)是@OP:您是否完全按照此处所示测试了这些行?在这两者之间没有其他代码?@Paul让它变得更简单:分配字符串不起作用。请参阅更新。@phant0m是,与此处所示完全相同。连线我知道。我添加了类定义$这个->产品是一组产品。我在公共函数&offsetGet($offset){
中添加了一个&in,现在它可以正常工作了。太好了,非常感谢!