how to merge words in php array
hi i have an array after doing explodes and other stuff in a following format.
[556] => Thorold
[557] => $130
[558] => Tilbury
[559] => $420
[560] => Tillsongbury
[561] => $225
[570] => Toronto
[571] => (Danforth
[572] => Area)
[573] => $55
[574] => Toronto
[575] => (Davisville
[576] => Area)
[577] => $50
[578] => Toronto
[579] => (Downtown
[580] => Area)
[581] => $50
[582] 开发者_如何学编程=> Toronto
[583] => (East
[584] => York
[585] => Area)
[586] => $60
i want to group the words in a single index and the price in the next one i.e. $data[0] = Toronto ( Downtown Area) $data[1] = $50.00
please suggest or help out.
<?
$source_array = array( 'A', 'B', '$50', 'C', 'D', 'E', '$200');
var_dump($source_array);
$portion = '';
$temp = array();
$result = array(array_pop($source_array));
while($item = array_pop($source_array)) {
if(substr($item, 0, 1) == '$') {
array_unshift($result, $item, implode($temp, ' '));
$temp = array();
$portion = '';
} else {
array_unshift($temp, $item);
}
}
array_unshift($result, implode($temp, ' '));
var_dump($result);
If your string is like this (which it seems to be)
$str = "Toronto (Danforth Area) $55 Toronto (Davisville Area) $50";
and the city names (or part of them) never end with a number, you can use preg_split
to get the desired structure:
preg_split('/\s+(?=\$)|(?<=\d)\s+/', $str);
gives
Array
(
[0] => Toronto (Danforth Area)
[1] => $55
[2] => Toronto (Davisville Area)
[3] => $50
)
Something like this should work:
$newArray = array();
for ($i = 0, $max = count($oldArray); $i < $max; $i++) {
$key = '';
while (substr($oldArray[$i], 0, 1) <> '$') {
$key .= ' ' . $oldArray[$i];
$i++;
}
$newArray[trim($key)] = $oldArray[$i];
}
I must say I agree with Felix's thinking, and that is probably the method (or similar) that I would be using if this was my project.
However, in the absence of seeing what explosions and other actions you are performing to prepare your data, this is what I would suggest:
Code: (Demo)
$location = '';
$result = [];
foreach ($array as $v) {
if (substr($v, 0, 1) != '$') {
$location .= ($location ? ' ' : '') . $v; // concatenate
} else {
array_push($result, $location, $v); // store location then dollar amount
// or make associative... $result[$location] => $v;
$location = ''; // restart
}
}
var_export($result);
Output:
array (
0 => 'Thorold',
1 => '$130',
2 => 'Tilbury',
3 => '$420',
4 => 'Tillsongbury',
5 => '$225',
6 => 'Toronto (Danforth Area)',
7 => '$55',
8 => 'Toronto (Davisville Area)',
9 => '$50',
10 => 'Toronto (Downtown Area)',
11 => '$50',
12 => 'Toronto (East York Area)',
13 => '$60',
)
Here is my version of a regex method: (Demo)
$string = 'Thorold $130 Tilbury $420 Tillsongbury $225 Toronto (Danforth Area) $55 Toronto (Davisville Area) $50 Toronto (Downtown Area) $50 Toronto (East York Area) $60';
var_export(preg_split('/ ?(\$\d+) ?/', $string, 0, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY));
// same output as my above non-regex method
This uses a simpler pattern and leverages the function's flags to ensure that the dollar amount is not omitted and that no empty elements are produced.
精彩评论