note 47866 modified in function.xml-parse-into-struct by sobak

From: Date: Sat, 10 Dec 2016 23:30:09 +0000
Subject: note 47866 modified in function.xml-parse-into-struct by sobak
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-208471@lists.php.net to get a copy of this message
If you have de XML: <?xml version="1.0" encoding="iso-8859-1"?> <teste> <teste> <teste> </teste> </teste> </teste> this function will return this tags: Array ( [teste] => Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 ) ) With this function (right below) will convert the struc to OPEN and CLOSE concept. The New Struct: Array ( [teste] => Array ( [0] => 0 [1] => 4 [2] => 1 [3] => 3 [4] => 2 ) ) The Function: <?php /** * @return bool * @param xml_array $values * @param xml_array[reference] $tags * @desc Reconstruct xml_parse_into_struct */ function reconstruct_tag($values, &$tags) { unset($return); foreach ($tags as $tkey => $tvalue) { foreach ($tvalue as $vvalue) { if ($values[$vvalue]['type'] == 'open' or $values[$vvalue]['type'] == 'complete') { $return[$tkey][] = $vvalue; } if ($values[$vvalue]['type'] == 'open') { foreach ($tvalue as $tv_value) { if ($values[$tv_value]['type'] == 'close' and $values[$tv_value]['level'] == $values[$vvalue]['level'] and $tv_value > $vvalue) { $return[$tkey][] = $tv_value; break; } } } } } $tags = $return; return (bool) $tags; } ?> Ahh, DO NOT forget this comment: jukkaho at mail dot student dot oulu dot fi 29-Jun-2004 12:17 Everyone seems to forget the return value of this function (including people making examples!), but it is there for a reason: 0 if failed (invalid XML), 1 if succeeded. Return value really is an integer, be careful with those === operators. --was-- If you have de XML: <?xml version="1.0" encoding="iso-8859-1"?> <teste> <teste> <teste> </teste> </teste> </teste> this function will return this tags: Array ( [teste] => Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 ) ) With this function (right below) will convert the struc to OPEN and CLOSE concept. The New Struct: Array ( [teste] => Array ( [0] => 0 [1] => 4 [2] => 1 [3] => 3 [4] => 2 ) ) The Function: /** * @return bool * @param xml_array $values * @param xml_array[reference] $tags * @desc Reconstruct xml_parse_into_struct */ function reconstruct_tag($values, &$tags) { unset($return); foreach ($tags as $tkey => $tvalue) { foreach ($tvalue as $vvalue) { if ($values[$vvalue]['type'] == 'open' or $values[$vvalue]['type'] == 'complete') { $return[$tkey][] = $vvalue; } if ($values[$vvalue]['type'] == 'open') { foreach ($tvalue as $tv_value) { if ($values[$tv_value]['type'] == 'close' and $values[$tv_value]['level'] == $values[$vvalue]['level']) { $return[$tkey][] = $tv_value; } } } } } $tags = $return; return (bool) $tags; } Ahh, DO NOT forget this comment: jukkaho at mail dot student dot oulu dot fi 29-Jun-2004 12:17 Everyone seems to forget the return value of this function (including people making examples!), but it is there for a reason: 0 if failed (invalid XML), 1 if succeeded. Return value really is an integer, be careful with those === operators. http://php.net/manual/en/function.xml-parse-into-struct.php

« previous php.notes (#208471) next »