drewgun Posted December 6, 2009 Share Posted December 6, 2009 I want to add an element to an array depending on the input of a user... I have no idea how to approach this but this is what I have so far... <?php $array[] array_push($array, $add_element); $input = $_POST["input"]; if ($input == "blah") { $add_element = "blahblahblah"; } ?> The problem with this was that instead of adding a new element for each imputed "blah" the element would just change. I'm obviously new at this and am completely lost. Any help is greatly appreciated. Link to comment https://forums.phpfreaks.com/topic/184188-adding-an-element-to-an-array-depending-on-user-input/ Share on other sites More sharing options...
smerny Posted December 6, 2009 Share Posted December 6, 2009 This must not be all of your code I guess? But wouldn't you want array_push($array, $add_element); to be after if ($input == "blah") { $add_element = "blahblahblah"; ? Link to comment https://forums.phpfreaks.com/topic/184188-adding-an-element-to-an-array-depending-on-user-input/#findComment-972431 Share on other sites More sharing options...
Brandon_R Posted December 6, 2009 Share Posted December 6, 2009 Using the function you specified <?php $input = $_POST['input']; $array = array(); if ($input == 'add_element') { $add_element = 'add_element_val'; $array = array_push($array, $add_element); } ?> Or you could simply add onto the end of the array like so <?php $input = $_POST['input']; $array = array(); if ($input == 'add_element') { $add_element = 'add_element_val'; $array[] = $add_element; } ?> Link to comment https://forums.phpfreaks.com/topic/184188-adding-an-element-to-an-array-depending-on-user-input/#findComment-972463 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.