Reputation: 311
I am passing an array via $_POST
method. I want to eliminate the spaces from the index values. My array is
Array
(
[full_word] => hi there
[btn] =>
)
I want to eliminate the space from the index element of full_word
so that I can get the array below:
Array (
[0]=> hi
[1]=> there
)
But explode function is not working in here as $_POST
returns an array and explode
simply doesn't work on arrays.
What's the solution?
Upvotes: 1
Views: 104
Reputation: 74
Use the below code to explode only 'full_word' section of $_POST array,
$full_word_array = explode(" ",$_POST['full_word']); var_dump($full_word_array);
Upvotes: 1
Reputation: 12937
You can explode()
around " "
using array_walk
. Eg below:
$arr = [ "full_word" => "hi there", "btn" => "" ];
// explode each value of array around " "
array_walk($arr, function(&$v) {
$v = explode(" ", $v);
});
print_r($arr);
// Array ( [full_word] => Array ( [0] => hi [1] => there ) [btn] => Array ( [0] => d ) )
Read more:
Upvotes: 2