I have a PHP variable that holds similar data:
$var = "
Name: Tom
Age: 26
Location: London
Name: Mike
Age: 28
Location: New York
Name: Sunny
Age: 24
Location: Tokyo";
I'd like to extract only the locations, and store them in Array like:
Array
(
[0] => London
[1] => New York
[2] => Tokyo
)
For now, with my simple experience with PHP I'm getting this done using "LOL, Don't Laugh..":
$location = array_merge(preg_replace('/^Location: /','',preg_grep('/^Location: /',explode("\n", $var))));
But I believe, there would be a way better process (maybe with preg_split
) to achieve that faster, wiser and with maybe less system over-load.
Kindly, advise.. Thanks in advance.. :-)