0

I have a weird bug, I dont know why it will not work. So I am using Laravel php framework and getting from db a object. Since some decimal number looks like this : '.00', I want to change it to this '0', but it cannot be changed when I loop through it. It looks like this:

 $stipendien = Stipendium::where('id','=',$request->input('id'))->get()->toArray();


        
        foreach($stipendien as $object => $test)
        {
            foreach($test as $key => $value)
            {
                if($test[$key] == ".00")
                {
                    $test[$key] = "0";
                }
            }
        }
return $stipendien;

Should not the '$stipendien' be changed when I assign the value new, since it does go to the if statement and getting the correct key ?

array:2 [
  0 => array:17 [
    "id" => 1
    "Kosten_Kurs" => "22.00"
    "Kosten_Prüfung" => ".00"
    "Kosten_Unterlagen" => ".00"
    "Kosten_Lernurlaub" => ".00"
    "Kosten_Reise" => ".00"
    "Kosten_Sonstig" => ".00"

  ]
  1 => array:17 [
    "id" => 2
    "Kosten_Kurs" => "22.00"
    "Kosten_Prüfung" => "2.00"
    "Kosten_Unterlagen" => ".00"
    "Kosten_Lernurlaub" => ".00"
    "Kosten_Reise" => ".00"
    "Kosten_Sonstig" => ".00"
  ]
]

But when I return it, it did not change at all. What did I wrong here?

4
  • Are you changing/updating $stipendien anywhere? Commented Dec 30, 2021 at 10:44
  • wouldnt $stipendien be changed, when I use the key and value iteration like this? Commented Dec 30, 2021 at 10:48
  • No, simplified, you take "the contents" of $stipendien[0] and changed that. $stipendien[0] itself stays untouched Commented Dec 30, 2021 at 10:50
  • Thank you! I really thought it would be changed and was confused like hell... Commented Dec 30, 2021 at 10:54

2 Answers 2

3

check that your condition is matched by adding dd() or die inside this if

if($test[$key] == ".00")
{
   $test[$key] = "0";
}
Sign up to request clarification or add additional context in comments.

3 Comments

hey! Yes it does die inside
try updating with $stipendien[$object][$key] = "0";
Thanks! It works! I thought it would change the object anyway when I loop through it...
2

You may try using this in loop while assigning 0

 $stipendien[$object][$key] = "0";

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.