Ozbodd
Ozbodd

Reputation: 73

PHP memory limit on line running foreach

Is there a more efficient way to set JSON array values than this?

    for($i=0;$i<sizeOf($json['activity']);$i++){
        $json['activity'][$i]['active'] = 'false';
}

I want to set all sub keys named 'active' to 'false' The arrays are not huge, they are multi-dimension with about 8-10 sub arrays and I am running on XAMPP localhost.

I am getting

Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes)

error briefly and then the rest of the code runs OK on a setInterval. I have looked at ways to set the memory limit but suspect there must be a cleaner way to set the array keys.

Thank you

Upvotes: 0

Views: 195

Answers (1)

RPaul
RPaul

Reputation: 66

If I understand this correctly, you created an infinite loop since everytime it runs, your array gets one more value, same as your $i-counter. Try getting the count of the array first in a seperate variable and then run the loop with that

$c = sizeOf($json['activity']); for($i=0;$i<$c;$i++){
  $json['activity'][$i]['active'] = 'false';
}

Upvotes: 1

Related Questions