Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9106155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:16:19+00:00 2026-06-17T02:16:19+00:00

i have a question regarding a foreach loop in PHP. For some reason i

  • 0

i have a question regarding a foreach loop in PHP.
For some reason i seem to only get the last line echo’ed back this way and i am lost in where this problem lies. Whenever i check $onderdeeltje[1] and $selectedmodel in an echo inside the foreach loop and outside the IF statement it pastes them all out perfectly without any problems. It also shows that they are equal and the same and should react on the IF statement.. As soon as i compare the two inside IF statement it shows me only the last one. Whenever i remove the checkbox echo and replace it with anything such as echo “test”; it also shows me test only once. Could anybody shed some light on this? I have multiple (2 + this one) foreach loops like this in the same page that work perfectly but only this one seems to bug.

echo "<form method='post' action='formpie.php'>";

$onderdelen = file("onderdelen.txt");

foreach ($onderdelen as $onderdeel) 
{
    $onderdeeltje = explode(",", $onderdeel);

    if ($onderdeeltje[1] == $selectedmodel)
    {
        echo "<input type='checkbox' name='check_list[]' value='$onderdeeltje[0]'>$onderdeeltje[0]<br>";
    }
}

echo "</form>";

$selectedmodel will be: iPhone 3G and the text list is this:

Reparatie touchscreen €41.50,iPhone 3G
Reparatie Wi-Fi-antenne €50.00,iPhone 3G
Reparatie trilmodule €40.00,iPhone 3G
Reparatie sim slot €70.50,iPhone 3G
Reparatie oorluidspreker €46.00,iPhone 3G

It only shows: Reparatie oorluidspreker €46.00 as a checkbox.

Please help me out! 🙂 Thanks in advance!

————————————————————-

How to solve:

It seems that there can be hidden spaces in the ouput that are not visible if these are pasted per line with things like <br> or in my case a list of repeated form checkbox values.

To fix this you simply have to trim the value before or inside the IF equal statement like so:

foreach ($onderdelen as $onderdeel) 
{
    $onderdeeltje = explode(",", $onderdeel);

    if (trim($onderdeeltje[1]) == $selectedmodel)
    {
        echo "<input type='checkbox' name='check_list[]' value='$onderdeeltje[0]'>$onderdeeltje[0]<br>";
    }
}

Solved thanks to support from ZombieSpy.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T02:16:20+00:00Added an answer on June 17, 2026 at 2:16 am

    As Marc B said, you might have the newline stuff, so use str_replace

    echo "<form method='post' action='formpie.php'>";
    
    $onderdelen = file("onderdelen.txt");
    
    foreach ($onderdelen as $onderdeel) 
    {
        $onderdeeltje = explode(",", $onderdeel);
    
        if (str_replace ("\n", "", $onderdeeltje[1]) == $selectedmodel)
        {
            echo "<input type='checkbox' name='check_list[]' value='$onderdeeltje[0]'>$onderdeeltje[0]<br>";
        }
    }
    
    echo "</form>";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question regarding db queries inside of a foreach loop. Im making
I have a question regarding the proper way to modify a php DateTime object.
I have a scope question regarding Linq expressions that are defined in a loop.
I have a question regarding construct HTML tags on back-end code (c#) in my
As an addendum to this question Java loop efficiency ("for" vs. "foreach") I have
I have a question regarding the native Array.forEach implementation of JavaScript: Does it behave
I have a question regarding smarty templates, I'm using PHP and Smarty in order
I have a question regarding reading an xml file with php. I have a
I have read some topic regarding with my question but there code doesn't work
I have question regarding the SQLAlchemy. How can I add into my mapped class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.