我有一个对象 $obj,它是 json_encode 的结果,其结构类似于:
stdClass Object (
[page] = 1
[size] = -1
[total] = 4
[collection] = Array
(
[0] = stdClass Object
(
[id] = 4e87de8e1a8840999f907fad
[description] = ffffffff
[code] = 82666
[status] = ACTIVE
)
[1] = stdClass Object
(
[id] = 4e8b4b53fda5efeeec370e89
[description] = gggggggggg
[code] = 41874
[status] = ACTIVE
)
[2] = stdClass Object
(
[id] = 4e8b4b5afda5efeeec370e8a
[description] = hhhhhhhhhhhhh
[code] = 15130
[status] = ACTIVE
)
[3] = stdClass Object
(
[id] = 4e90a753e91a2713c95b56a0
[description] = jjjjjjjjjjjjjj
[code] = 323307
[status] = ACTIVE
)
[4] = stdClass Object
(
[id] = 4e9e08da79597f0d3daba159
[description] = kkkkkkkkkkkkkk
[code] = 11310
[status] = ACTIVE
)
) )
我有一个值 $code 并想在 $obj->collection 中搜索,将 $id = 设置为 code 属性等于 $code 值的对象的 id 属性。我现在用一个条件循环来检查每个的代码属性并设置 $id 如果为真,但我认为可能有一个命令我错过了由于对 OO PHP 相当不熟悉。
有什么办法可以改进吗?