1

I have problem in modifying the value of the array of objects.

array (size=2)
  0 => 
    object(stdClass)[25]
      public 'time1' => string '09:00:00' (length=8)
      public 'btm_01' => string '40.00' (length=5)
      public 'bto_01' => string '41.00' (length=5)
      public 'rs_01' => string '42.00' (length=5)
  1 => 
    object(stdClass)[26]
      public 'time1' => string '10:00:00' (length=8)
      public 'btm_01' => string '41.00' (length=5)
      public 'bto_01' => string '40.00' (length=5)
      public 'rs_01' => string '40.00' (length=5)

I need a for loop to remove '.00' in each value the array of objects. To remove '.00' it's easy matter but after I can remove it, I still can't replace the old value with the new one in array of objects.

Could you help me, how to modify the value in array of objects with PHP?

Thanks!

3 Answers 3

4

Nah,

It so simple.. I'll give you an example

 foreach($data['bottom_max'] as $key => $value)
 {
     foreach ($value as $name_row => $val_row) {
         if (strpos($val_row, '.0')) {
             $tmp = substr($val_row, 0, -3);
             $data['bottom_max'][$key]->$name_row = $tmp;
         }
     }
 }

Hope it's answer your question.

Sign up to request clarification or add additional context in comments.

Comments

1

try this (access the obejct by reference):

foreach($array as & $obj) {
    $obj->time1 = $newvalue;
}

Comments

0

Easy :

<?php
$object[0]->time1 = '10:00:00';
?>

It just like what you imagine

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.