TwoStarII TwoStarII - 4 months ago 6
PHP Question

Get Each Item In An Array From User Input

I would like to get each item in an array from user input form. The user will input numbers from a single input field separated by space (1 2 3 4) then I get this input and replace the space with commas (1,2,3,4) and add this variable to array however the results returned by $number seems to return the entire value as a single number like this:

1,2,3,4


And not like this:

1
2
3
4


code:

<?php

$inp_results =$_POST['inp_results'];
$inp_results_comma = str_replace(' ', ',', $inp_results);
$number_in_results = array($inp_results_comma);

foreach ($number_in_results as $number)
{
echo "$number</br>";
}

Answer
$inp_results = $_POST['inp_results'];
$number_array = explode(' ', $inp_results);
foreach ($number_array as $number) {
    echo "$number</br>";
}
Comments