Make a string from an array

Hello,

I need to make a variable string from an array ‘$regions’

The string needs to end up like this:
[_title]|[areaSlug], [_title]|[areaSlug], etc for each item in the array

Can anyone help me with the code to achieve this if this was the array:

Array (
[0] => Array ( [_id] => 235 [areaSlug] => head-office [region-area-list] => Head Office [_title] => Head Office [_page] => /careers/homecare/vacancies.php )
[1] => Array ( [_id] => 236 [areaSlug] => merton [region-area-list] => Merton [_title] => Merton [_page] => /careers/homecare/vacancies.php )
[2] => Array ( [_id] => 237 [areaSlug] => wandsworth [region-area-list] => Wandsworth [_title] => Wandsworth [_page] => /careers/homecare/vacancies.php )
[3] => Array ( [_id] => 238 [areaSlug] => sutton [region-area-list] => Sutton [_title] => Sutton [_page] => /careers/homecare/vacancies.php )
[4] => Array ( [_id] => 239 [areaSlug] => southwark [region-area-list] => Southwark [_title] => Southwark [_page] => /careers/homecare/vacancies.php )
[5] => Array ( [_id] => 240 [areaSlug] => ealing [region-area-list] => Ealing [_title] => Ealing [_page] => /careers/homecare/vacancies.php )
[6] => Array ( [_id] => 241 [areaSlug] => brent [region-area-list] => Brent [_title] => Brent [_page] => /careers/homecare/vacancies.php )
[7] => Array ( [_id] => 242 [areaSlug] => croydon [region-area-list] => Croydon [_title] => Croydon [_page] => /careers/homecare/vacancies.php ) )

There ya go:
[php]$str = ‘’;
for($i=0; $i < sizeof($regions); $i++) {
$str .= $regions[$i][’_title’] . ‘|’ . $regions[$i][‘areaSlug’] . ‘,’;
}
// remove the last comma
$str = substr($str, 0, -1);

echo $str;
// output: Head Office|head-office,Merton|merton
[/php]

Hope that helps,
Red :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service