array { [0]=>
City
Street
Zip
I have this but what I need is something like:
array { [0]=>
'city'=>City
'Street'=>Street
'zip'=>Zip
I am using PHPDOM to get the html which I than add to an array
$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('div') as $element) {
if (strpos($element->getAttribute('class'), 'contentSection') !== false) {
$string = $element->C14N();
}
}
$array = explode("</p>", $string);
print_r($array);
HTML part that I am processing with DOM
<div class="contentSection">
City
<br>
Street
<br>
ZIP
<p class="Separator">
</p>
City
<br>
Street
<br>
ZIP
Something that I tried to do is
$dom = new DOMDocument('1.0');
$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('div') as $element) {
if (strpos($element->getAttribute('class'), 'contentSection') !== false) {
$string = $element->C14N();
}
}
$keys = array("city","street", "zip");
$array = array_explode_with_keys("<br>", $keys, $string);
print_r($array);
function array_explode_with_keys($delimiter, $keys, $string){
$return = array();
$pieces = explode($delimiter,$string);
foreach($pieces as $i=>$piece){
if($i<count($keys)) {
$return[$keys[$i]] = $piece;
} else {
$return[$i] = $piece;
}
}
return $return;
}
?>
but this returns:
Array ( [city] =>
city[street] =>
street [zip] =>
zip
city [3] =>
street [4] =>
zip
So it goes wrong in the second part