Toeuwer3 Toeuwer3 - 13 days ago 6
PHP Question

Storing exploded string in array

How can I store the explode function output in an array and use it like " $MessageArray[$0]" ?

Here is my code :

$SelectedMessage = GETTING DATA FROM DB

$MessageArray = print_r ( explode("♠", $SelectedMessage), true);
$MessageArraySize = count($MessageArray);

for ($x = 0; $x <= $MessageArraySize; $x++) {
echo 'Message: '.$MessageArray[$x];
}


output: Arr (because it doesent store the output from exlode as an ARRAY, it stores it as text)

Answer

The problem is not with explode, but with print_r. You should rather do it separately

$MessageArray = explode("&#9824;", $SelectedMessage);
print_r($MessageArray, true);

This way, you capture the resulting array from explode and then show it with print_r. The other way round, you will give the array to print_r and receive a string as return from print_r, which is not what you want.

return
If you would like to capture the output of print_r(), use the return parameter. When this parameter is set to TRUE, print_r() will return the information rather than print it.

Comments