3

I want to explode coma from an array value. My code is.

        $to_ids_string = "";            
        $to_id = $this->input->post('to');            
        for ($r = 0; $r < count($this->input->post('to')); $r++) {
            if ($to_ids_string != "") {
                $to_ids_string = $to_ids_string . "," . $to_id[$r];
            } else {
                $to_ids_string = $to_id[$r];
            }                
        }
       echo $to_ids_string;
            $a = explode(',', $to_ids_string);
            foreach ($a as $item) {
                echo("<li>$item</li>");
                exit;
            }

when i echo $to_ids_string it will return 2,3 but when i loop in foreach it only return 2 not show 3.

2
  • remove exit; from foreach. add it after completion of foreach. You are breaking it after one iteration Commented Jul 12, 2017 at 10:25
  • remove exit and print the $a array to see there are one element or two element Commented Jul 12, 2017 at 10:28

3 Answers 3

2

Because of your exit, if you use exit like that, then it is the end of your program and it doesn't echo anymore.

Sign up to request clarification or add additional context in comments.

Comments

1

You forget to remove exit; from foreach loop. When you write exit, execution of your code stops. Hence you are not getting desired output.

Comments

1

Happens due to exit. Please remove exit from your code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.