Answer the question
In order to leave comments, you need to log in
How to convert HTML tag tree to PHP array?
Good evening. There is some HTML file, taken by the file_get_contents function and printed by print_r ($file);
As a result, we get content with the following HTML structure:
Task: convert the given structure into a PHP array and separate each tag into elements in the array.
What I did:
I tried to convert the structure to an array using the following code:
$buffer = file_get_contents("ссылка");
function XML2Array(SimpleXMLElement $parent)
{
$array = array();
foreach ($parent as $name => $element) {
($node = & $array[$name])
&& (1 === count($node) ? $node = array($node) : 1)
&& $node = & $node[];
$node = $element->count() ? XML2Array($element) : trim($element);
}
return $array;
}
$xml = simplexml_load_string($buffer);
$array = XML2Array($xml);
$array = array($xml->getName() => $array);
var_dump($array);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question