How do I create variables from XML data in PHP?

后端 未结 9 1590
不思量自难忘°
不思量自难忘° 2021-01-25 18:34

Been trying to figure this out for a short while now but having now luck, for example I have an external xml document like this:


&         


        
9条回答
  •  说谎
    说谎 (楼主)
    2021-01-25 19:02

    From the PHP web site at http://www.php.net/manual/en/function.xml-parse.php:

    Ashok dot 893 at gmail dot com 26-Apr-2010 05:52 This is very simple way to convert all applicable objects into associative array. This works with not only SimpleXML but any kind of object. The input can be either array or object. This function also takes an options parameter as array of indices to be excluded in the return array. And keep in mind, this returns only the array of non-static and accessible variables of the object since using the function get_object_vars().

     $value) {
                if (is_object($value) || is_array($value)) {
                    $value = objectsIntoArray($value, $arrSkipIndices); // recursive call
                }
                if (in_array($index, $arrSkipIndices)) {
                    continue;
                }
                $arrData[$index] = $value;
            }
        }
        return $arrData;
    }
    ?>
    
    Usage:
    
    
    

    This will give the following result:

    Array
    (
        [name] => My Template Name
        [author] => John Doe
        [positions] => Array
            (
                [position] => Array
                    (
                        [0] => top-a
                        [1] => top-b
                        [2] => sidebar-a
                        [3] => footer-a
                    )
    
            )
    
    )
    

提交回复
热议问题