i have two foreach operations like below.
$sql = "select region_name from ".$GLOBALS['ecs']->table('region') . " where region_id in(".$order['province'].",". $order['city'].",".$order['district'].")";
$address = $GLOBALS['db']->getAll($sql);
foreach($address as $vo){
$region .= $vo['region_name'];
}
$order['1address'] = $region.$order['1address'];
return $order;
}
Here's a sample of the output data:
AlabamaColorado
How i want it to be
Alabama Colorado
The only thing wrong is I could not separate Alabama and Colorado with blank space. It might be an easy operation but how? Any idea?
Thanks a lot.
try this:
if ($region == '') {
$region = $vo['region_name'];
} else {
$region .= ' ' . $vo['region_name'];
}
You can simply do that by concatenating a space between your $region
and $order['1address']
variables.
The snippet below illustrates how you can achieve that:
$order['1address'] = $region . " " . $order['1address'];
You can try this without foreach,
$region = explode(' ', array_column($address, 'region_name'));
$order['1address'] = $region . $order['1address'];
In cases like this, I prefer to use an array and then implode it:
// initialize the array to avoid NOTICES
$region = array();
foreach ( $address AS $vo ) {
// add each region name to a new element in the array
$region[] = $vo['region_name'];
}
// implode the regions array to a string separated by a space
$region = implode( ' ', $region );