I have a table in a HTML file, using my CODE below I have managed to get all values from this table as a Array.
The problem is my CODE makes Array as: (Key -> Value) (Key -> Value). But the structure of my data is: (Key -> Value, Value) (Key -> Value, Value)
Here is Content of: myHTMLfile.html
<td height="22" align="center" bgcolor="#FFFFFF">1 Hour </td>
<td align="center" bgcolor="#FFFFFF">400 USD</td>
<td align="center" bgcolor="#FFFFFF">450 USD</td>
<td height="22" align="center" bgcolor="#FFFFFF">2 Hours</td>
<td align="center" bgcolor="#FFFFFF">500 USD</td>
<td align="center" bgcolor="#FFFFFF">600 USD</td>
<td height="22" align="center" bgcolor="#FFFFFF">3 Hours </td>
<td align="center" bgcolor="#FFFFFF">600 USD</td>
<td align="center" bgcolor="#FFFFFF">700 USD</td>
Im using this CODE to get content and make this Array:
$file = ("myHTMLfile.html");
$searchfor = 'align="center" bgcolor="#FFFFFF"';
header('Content-Type: text/html');
$html = file_get_contents($file);
$pattern = preg_quote($searchfor, '/');
$pattern = "/^.*$pattern.*\$/m";
if(preg_match_all($pattern, $html, $matches));
function remap_alternating(array $values) {
$remapped = array();
for($i = 0; $i < count($values) - 1; $i += 2) {
$remapped[strip_tags(trim($values[$i]))] = strip_tags(trim($values[$i + 1]));
}
return $remapped;
}
$mapped = remap_alternating($matches[0]);
// RAM
$keys = array_map("trim", array_map("strip_tags", array_keys($mapped)));
$values = array_map("trim", array_map("strip_tags", array_values($mapped)));
$mapped = array_combine($keys, $values);
Below you can see the results I get from var_dump($mapped);
array(4) {
["1 Hour"]=>
string(7) "400 USD"
["450 USD"]=>
string(7) "2 Hours"
["500 USD"]=>
string(7) "600 USD"
["3 Hours"]=>
string(7) "600 USD"
}
I want this Array results to look like:
array(3) {
["1 Hour"]=>
string(16) "400 USD, 450 USD"
["2 Hours"]=>
string(16) "500 USD, 600 USD"
["3 Hours"]=>
string(7) "600 USD"
}
My question is: What is the correct PHP programming CODE to get the results as I want?
UPDATED BELOW
I found a example and it looks like its what I need. But how can I use it with my CODE above?
Example:
$get = "first=value1&second=value2&third=value3";
print_r(array_map2("explode","=",explode("&",$get)));
would print out:
Array
(
[0] => Array
(
[0] => first
[1] => value1
)
[1] => Array
(
[0] => second
[1] => value2
)
[2] => Array
(
[0] => third
[1] => value3
)
)