1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Does foreach() work within for()?

Discussion in 'PHP' started by qwikad.com, May 2, 2025.

  1. #1
    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()
     
    qwikad.com, May 2, 2025 IP
  2. Spoiltdiva

    Spoiltdiva Acclaimed Member

    Messages:
    7,827
    Likes Received:
    2,963
    Best Answers:
    53
    Trophy Points:
    520
    #2
    No, the foreach () method is an iterative method and always returns undefined and is not chainable.
     
    Spoiltdiva, May 2, 2025 IP
  3. Sumit_Singh

    Sumit_Singh Well-Known Member

    Messages:
    737
    Likes Received:
    65
    Best Answers:
    6
    Trophy Points:
    100
    #3
    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
     
    Sumit_Singh, May 3, 2025 IP
  4. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,255
    Likes Received:
    1,690
    Best Answers:
    31
    Trophy Points:
    475
    #4
    I see the problem. The array in $_POST['del'] has undefined keys. Need to figure out why.
     
    qwikad.com, May 3, 2025 IP
  5. Sumit_Singh

    Sumit_Singh Well-Known Member

    Messages:
    737
    Likes Received:
    65
    Best Answers:
    6
    Trophy Points:
    100
    #5
    Can you share the code?
     
    Sumit_Singh, May 3, 2025 IP
  6. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,255
    Likes Received:
    1,690
    Best Answers:
    31
    Trophy Points:
    475
    #6
    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.
     
    qwikad.com, May 3, 2025 IP
  7. Sumit_Singh

    Sumit_Singh Well-Known Member

    Messages:
    737
    Likes Received:
    65
    Best Answers:
    6
    Trophy Points:
    100
    #7
    Great!
     
    Sumit_Singh, May 3, 2025 IP
  8. OnlineProxyIo

    OnlineProxyIo Greenhorn

    Messages:
    93
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    18
    #8
    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.
     
    OnlineProxyIo, May 3, 2025 IP