For some reason can't make it work, and since I don't know why, I want to ask if this should work: for($i=0; $i < $total; $i++ ) { ...some code foreach($_POST['del'] as $pics) { ...some code } ...some code } Everything works fine when foreach() is outside of for()
Yes, it works in PHP. Spoiltdiva is right either, it is an iterative method and returns undefined but in JavaScript and not chainable but in JS. Check for the following if it is not working If you're testing this before the form is submitted, $_POST['del'] won't exist. Check if $total is 0 or negative (loop is never entered) Logic inside the loop can affect each (check the logics carefully) Ensure error_reporting(E_ALL); and ini_set('display_errors', 1); are enabled so you can catch issues. $_POST['del'] is not set or not an array
I'd rather not, but I can see why it's happening. The foreach keeps looping the same variables. I need to figure out how to do it just once. Any ideas? *Edit* I got it. Using break; stopped it from repeating.
Absolutely, you can use foreach inside a for loop in PHP, no problem there. If it's not working for you, chances are the issue's with $_POST['del']. Double-check that it’s actually an array before you try looping over it. Also, pro tip: don’t reuse variable names like $i in both loops. That’ll trip you up real quick with conflicts you don’t wanna deal with. Keep things clean, and you'll be golden.
Yes, foreach() works inside a for() loop without issues. If it’s not working inside, check that $_POST['del'] is properly set and accessible during each iteration. Also, ensure there are no early continue or break statements affecting the loops. Debugging with var_dumps inside the loops can help pinpoint the problem.
Try the code below. if (isset($_POST['del']) && is_array($_POST['del'])) { for($i = 0; $i < $total; $i++) { // ...some code before foreach foreach($_POST['del'] as $pics) { // ...some code using $pics } // ...some code after foreach } }