Search code examples
phpmultidimensional-arrayphp-5.3

How to explode the value inside the item of an array?


I wonder how I can explode the value inside the item of an array, for instance,

This the $_POST data sent from a form,

[required] => Array
        (
            [0] => member_email
            [1] => member_birthday member_secret
        )

The script I use to split the required values into an array,

# Set the required array.
$items_variable = array(
    'required'
);

# Loop the array.
foreach($items_variable as $item_variable)
{
    # Set the main variables.
    $$item_variable = set_variable($_POST,$item_variable);
}

# Get the required field and put them in an array.
$array_required = is_array($required)? $required : explode(' ',$required);


print_r($array_required);

result,

Array
(
    [0] => member_email
    [1] => member_birthday member_secret
)

but this is the correct result I am after,

Array
    (
        [0] => member_email
        [1] => member_birthday
        [2] => member_secret
    )

Any ideas?


Solution

  • $required = array();
    foreach( $_POST['required'] AS $val )
      $required = $required + explode(' ', $val); // '+' means union in this case