This is my code:
<?php foreach ($classes as $str) foreach ($str as $class): ?>
...
<?php endforeach ?>
$str has just one string value with a random string index.
Can the first statement be optimized to reduce it to just one foreach?
1 Answer 1
You could flatten your classes array, but it wouldn't give you any performance benefit because internally, the flattening of an array will iterate over the entire array anyway.
The short answer is: no - there's no way to optimize this without changing the way that the $classes array is constructed (if that's possible) - assuming you need access to all of the 2nd-level children of the $classes array.
Edit: If your $str sub-array only has one random child with a random array index, you could use current($str) - it might prove to be SLIGHTLY faster than a foreach
$classes?$classesarray appears to be 2D, you'll probably need 2 loops. Really, we need to know what is in the array, and what you plan to do in the loop.