简体   繁体   中英

PHP: Object Array - Replace Key and Value

I have this object array $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) ""
  }
}

I'm trying to replace the serialised array in [hours] where [status] = open with a new seralised array $newSchedule by doing:

$work_breaks['hours'] = $newTimes;

but I can't seem to get it to work. How would I do a conditional check if [status] is 'open'?

As every item of array is stdClass object you should use -> instead of square brackets.

foreach ($work_breaks as $item) {
    if ($item->status == 'open') {
        // do something
        $item->hours = 'Your value';
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM