Bonsoir,
Depuis quelques jours, je me casse les dents sur du code PHP pour extraire des données d'un
fichier XML vers un tableau associatif PHP ...
Mon code PHP actuel :
private function xml_to_array
($elements) {
$array = array();
foreach($elements as $element) {
$name = filter_var(strip_tags($element->nodeName), FILTER_SANITIZE_STRING
);
if( $name != '#text') {
$headline = array();
if($element->childNodes->length != null) {
foreach($element->childNodes as $child) {
$childname = filter_var(strip_tags($child->nodeName), FILTER_SANITIZE_STRING
);
if($child->hasChildNodes()) {
$this->xml_to_array($child);
$headline[$childname] = $child->nodeValue;
}
}
}
$array[$name] = $headline;
}
}
unset($item); var_dump($array);
//if(!empty($array)) { return $array; unset($array); }
}
$elements = $dom->getElementsByTagName( 'datas' );
$version = $this->xml_to_array($elements);
Ce que je souhaite arriver à obtenir est ceci :
array(3) {
["zao"]=>
array(4) {
["version"]=>
string
(5) "0.1.5"
["date"]=>
string
(35) "2015-08-26 20:15:43.912878767 +0200"
["name"]=>
string
(25) "Z -> Another Object (web)"
["url"]=>
string
(32) "https://framagit.org/hucste/ZAO/"
}
["h5bp"]=>
array(3) {
["name"]=>
string
(16) "HTML5BoilerPlate"
["url"]=>
string
(29) "https://html5boilerplate.com/"
["version"]=>
string
(5) "5.2.0"
}
["bootstrap"]=>
array(4) {
["name"]=>
string
(9) "Bootstrap"
["url"]=>
string
(24) "https://getbootstrap.com"
["version"]=>
string
(5) "3.3.6"
["integrity"]=>
array(3) {
["css"] => string
() "sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7"
["js"] => string
() "sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS"
["theme"] => string
() "sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r"
}
}
}
Bref, je n'arrive pas à boucler, ou à trouver comment boucler correctement ...
Merci d'avance pour ceux qui m'aiguilleront !
Dernière modification par PengouinPdt (16-06-2016 14:08:52)