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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You could flatten your
classesarray, 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
$classesarray is constructed (if that’s possible) – assuming you need access to all of the 2nd-level children of the$classesarray.Edit: If your
$strsub-array only has one random child with a random array index, you could usecurrent($str)– it might prove to be SLIGHTLY faster than aforeach