How can you convert foreach -statements to for -loops in PHP?
Examples to be used in your answers
1
foreach( $end_array[1]['tags'] as $tag )
and
2
foreach($end_array as $question_id => $row2)
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.
In both examples the expressions left to ‘as’ refer to an array. An array stores a mapping of keys to values. Both examples iterate through elements of this mapping.
In the first example you are only interested in the values (and not in the keys they are mapped to). At every iteration $tag refers to the “current” value.
In the second example $question_id refers to the key, $row2 refers to the value of the “current” mapping.
In general the expression
could be rewritten as