Jump to content

Adding an element to an array depending on user input


drewgun

Recommended Posts

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.

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;
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.