There are two things to remember here:
explode()function returns an array, where each element is a “piece” from the explosion. None of those elements will contain the explosion string itself. So if I explode on the colon character by writing
explode(":", "here:are:four:words");, that will return an array of the four words themselves without any colons.
- If you want a particular element of an array, just access it using something like this:
$the_third_element_of_array_name = $array_name. The bracket syntax refers to a particular element in the array, so when you store it in a variable, it’s stored as a plain ol’ scalar variable, not an array.
// store a plain old sentence in $sentence $sentence = "First half of the sentence, second half of the sentence."; // explode $sentence into pieces wherever a comma appears, and store the (two) pieces that result as an array called $exploded_sentence $exploded_sentence = explode(", " , $sentence); // I just want the second piece of the explosion, so I'll access the second element of the array using $exploded_sentence (remember, the first piece can be accessed with $exploded_sentence) $second_half = $exploded_sentence; // $second_half is a plain ol' scalar variable (not an array), so we can run IT through explode(). Let's do so by exploding on the space character: this will return as many pieces as there are words in the sentence $second_half_exploded = explode(" ", $second_half); // $second_half_exploded is of course an array, so if we want the first word, we just access it at $second_half_exploded $first_word_second_half = $second_half_exploded; echo $first_word_second_half;