0

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

1
  • Have you try setting to false as a boolean? It would use less space than using false as string. Another optimization you can assign sizeof($json['activity']) in to a variable before using in the loop. Commented Sep 16, 2016 at 6:12

1 Answer 1

1

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';
}
Sign up to request clarification or add additional context in comments.

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.