【发布时间】:2021-11-28 13:09:33
【问题描述】:
我正在尝试迭代一个 php 对象并通过引用更改每个字符串值,但有些东西不起作用。在某些数组中,字符串不会更改。任何人都知道为什么?或者对如何解决任务有建议?
这是我的代码:
recursive_object_string_changer($object);
function recursive_object_string_changer($object)
{
if($object == null) {
return;
}
foreach ($object as &$attribute) {
if (is_string($attribute)) {
$attribute = $attribute."!";
} else if (is_array($attribute)) {
recursive_object_string_changer($attribute);
} else if (is_object($attribute)) {
recursive_object_string_changer($attribute);
}
}
unset($attribute);
}
非常感谢!
【问题讨论】:
-
你能用
stdClass做一个非常简单的示例对象供我们参考吗?
标签: php object reference iteration