Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to add user input to a previously created array with array_merge. However, I am having trouble echoing the entire, new array as an unordered list. the user's entry is being processed correctly, but the original array is displaying as "Array" within the unordered list. Here is the code:

<?php
$travel = array("Automobile", "Jet", "Ferry", "Subway");

foreach ($travel as $t)
    {
    echo "<ul>";

    echo "<li>$t</li>";

    echo "</ul>";

    }
?>

<form action="arrays.php" method="post">
<input type="text" name="added" />
<?php

foreach ($travel as $t)
{
echo "<input type=\"text\" name=\"travel[]\" value=\"$t\" />";      
}

?>
<input type="submit" name="submit" value="Add More!" />
</form>

<?php

$travel = array($_POST["travel"]);

$added = explode(",", $_POST["added"]);

$travel = array_merge($travel, $added);

echo "<p> Here is the list with your additions:</p>";

echo "<ul>";

foreach ($travel as $t)
{
echo "<li>$t</li>";
}

echo "</ul>";
?>
share|improve this question
This has been solved by removing "array()" from the first $travel variable and preceding it with if (isset($_POST["submit"])). I will post the code as an answer...thanks for the feedback everybody :) – dmubu Aug 6 '11 at 5:22

4 Answers

Try using a new variable name for the new array created by array_merge(). I think you may run into problems modifying the array you're storing into.

share|improve this answer
unfortunately, no luck with that approach...any other ideas? – dmubu Aug 6 '11 at 4:39
OH, I think you just need to change $travel = array($_POST["travel"]); to $travel = $_POST["travel"]; (in addition to using a new variable name). $_POST["travel"] is already an array. – Devin Ceartas Aug 6 '11 at 4:48
When I do that, I receive a warning stating that Arugment #1 is not an array... – dmubu Aug 6 '11 at 4:54
Not sure what you're doing. This works for me (tested): $travel = $_POST["travel"]; $added = explode(",", $_POST["added"]); $ntravel = array_merge($travel, $added); print_r( $ntravel ); – Devin Ceartas Aug 6 '11 at 4:55
I just copied and pasted your input...same warning...maybe warnings are turned off??? – dmubu Aug 6 '11 at 5:07

Use foreach on both of the arrays and add the information to a new array. Once this is done you can unset the previous arrays and carry on with your coding.

share|improve this answer
How exactly would I do this? – dmubu Aug 6 '11 at 4:49
$travel = array($_POST["travel"]);

should be

$travel = $_POST['travel'];
share|improve this answer
\"travel\" without [] will print only the last value of the array. – dmubu Aug 6 '11 at 4:48
Sorry i've been misslead by $added = explode(",", $_POST["added"]); what are you trying to do there ? Because explode takes a string as a second argument and $_post['added'] is an array – yokoloko Aug 6 '11 at 4:55
The user can add items to the first array by entering options, comma separated. Once submit is clicked, the old array plus the user's input should display as one, unordered list... – dmubu Aug 6 '11 at 5:04
ok i edited my answer you were having an array of array – yokoloko Aug 6 '11 at 5:28

The problem was solved thus:

if (isset($_POST["submit"]))
{
$travel = $_POST["travel"];
$added = explode(",", $_POST["added"]);
$travel = array_merge($travel, $added);


echo "<p> Here is the list with your additions:</p>";

echo "<ul>";

foreach ($travel as $t)
    {
    echo "<li>$t</li>";
    }

echo "</ul>";
}
?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.