我有这个对象数组$ work_breaks(Var_dump):
array(2) {
[0]=>
object(stdClass)#3717 (7) {
["ID"]=>
string(1) "7"
["location"]=>
string(1) "0"
["service"]=>
string(1) "0"
["worker"]=>
string(1) "3"
["status"]=>
string(4) "open"
["hours"]=>
string(624) "a:7:{s:6:"Sunday";a:3:{s:6:"active";s:2:"no";s:5:"start";s:5:"00:00";s:3:"end";s:5:"00:30";}s:6:"Monday";a:3:{s:6:"active";s:2:"no";s:5:"start";s:5:"00:00";s:3:"end";s:5:"00:30";}s:7:"Tuesday";a:3:{s:6:"active";s:2:"no";s:5:"start";s:5:"00:00";s:3:"end";s:5:"00:30";}s:9:"Wednesday";a:3:{s:6:"active";s:3:"yes";s:5:"start";s:5:"00:00";s:3:"end";s:5:"00:30";}s:8:"Thursday";a:3:{s:6:"active";s:2:"no";s:5:"start";s:5:"00:00";s:3:"end";s:5:"00:30";}s:6:"Friday";a:3:{s:6:"active";s:2:"no";s:5:"start";s:5:"00:00";s:3:"end";s:5:"00:30";}s:8:"Saturday";a:3:{s:6:"active";s:2:"no";s:5:"start";s:5:"00:00";s:3:"end";s:5:"00:30";}}"
["note"]=>
string(0) ""
}
[1]=>
object(stdClass)#3935 (7) {
["ID"]=>
string(1) "8"
["location"]=>
string(1) "0"
["service"]=>
string(1) "0"
["worker"]=>
string(1) "3"
["status"]=>
string(6) "closed"
["hours"]=>
string(37) "a:1:{s:16:"exceptional_days";s:0:"";}"
["note"]=>
string(0) ""
}
}
我试图用[hours]替换序列化数组,其中[status] = open,使用新的seralised数组$ newSchedule:
$work_breaks['hours'] = $newTimes;
但我似乎无法让它发挥作用。如果[状态]打开'?
,我该如何进行有条件检查答案 0 :(得分:1)
由于每个数组项都是stdClass
个对象,因此您应该使用->
而不是方括号。
foreach ($work_breaks as $item) {
if ($item->status == 'open') {
// do something
$item->hours = 'Your value';
}
}