Making an array from a string

I have read the llink https://www.w3schools.com/php/php_arrays.asp several times.
I undertand it.
But applying it in my application. I face difficulty.
Okay, I l am telling what I want in the bellow.

I have a string like the below.

$myString  =  "Volvo BMW";

There is a space between “Volvo” and “BMW”.
I guess the space can be a delimiter.

I like to make an array named “$cars” with $myString above.

code

echo $cars[1] ;

when I do the code above, I like to get the result below.

result

How can I get the result “BMW” above with the code “echo” above by starting $myString “Volvo BMW” above?

It appears to me that the example is not understood :slight_smile:

Try this:

echo '<pre>'; // adds line feeds

$sString =  "Volvo BMW Toyota Mercedes";
  echo 'var_dump($sString) ==> '; var_dump($sString);
  echo '<hr><br>';

$aCars = explode(' ', $sString);
  echo 'print_r($aCars) ==> '; print_r($aCars);
  echo '<hr><br>';

$sCars = implode($aCars);
  echo 'var_dump($sCars) ==> '; 
  echo '$aCars ==> '; var_dump($sCars);
  echo '<hr><br>';

die;

@joon1,

  1. Did you try the script?

  2. Did it solve your problem?

  3. Did you spot my deliberate mistake?

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.