简体   繁体   中英

Change string value of an array to another string value

I have something like this:

Collection {#296 ▼
#items: array:1318 [▼
0 => {#289 ▼
  +"Column1": "string1"
  +"Column2": "string2"
  +"Column3": "string3"
  +"Column4": "string4"
  +"Column5": "string5"
 }
1 => {#292 ▼
  +"Column1": "string6"
  +"Column2": "string7"
  +"Column3": "string8"
  +"Column4": "string9"
  +"Column5": "string10"
 }
2 => {#293 ▼
  +"Column1": "string11"
  +"Column2": "string12"
  +"Column3": "string13"
  +"Column4": "string14"
  +"Column5": "string15"
 }
]
}     

For each "Column1" I need to change the string value to another. Can you help me? Regards

I'll explain better now:

if Column1 is equal to a certain value, then Column2 is equal to my string

I have tried this:

 foreach($all as $key=>$row){
        if($row->Column1 = 'NUMAX'){
            $ipoltstatus = Str::before($row->message, '_');
            $olthostname = Olt::where('ipaddress', $ipoltstatus)->value('hostname');

            $row->Column2 = $olthostname;

        }

what happens is that all Column1 columns mistakenly take the NUMAX value and column2 becomes empty

In PHP comparison is done by === not = , a single equal sign is for setting a value. So changing = to === should do the trick. Also there is == and === , two equal signs type juggles and triple compares the variables as is. 2 == '2' is true and 2 === '2' is false.

foreach ($all as $key => $row) {
    if ($row->Column1 === 'NUMAX') {

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