Php 为函数提供的参数无效

Php 为函数提供的参数无效,php,Php,您好此函数正在抛出“为foreach()提供的参数无效”错误 $data可能不是数组或对象请参阅上面的完整源代码。我还想补充一点,这个脚本在PHP5.3.0(WAMP)中工作得很好,在PHP5.3.3(Linux/Apache)中抛出错误。我还想补充一点,这个脚本在PHP5.3.0(WAMP)中工作得很好,在PHP5.3.3(Linux/Apache)中抛出错误。这张海报做了什么来降级?我很好奇。如果你echo gettype($data)在flatte()里面,结果是什么?@octopusgr

您好此函数正在抛出“为foreach()提供的参数无效”错误


$data
可能不是数组或对象

请参阅上面的完整源代码。我还想补充一点,这个脚本在PHP5.3.0(WAMP)中工作得很好,在PHP5.3.3(Linux/Apache)中抛出错误。我还想补充一点,这个脚本在PHP5.3.0(WAMP)中工作得很好,在PHP5.3.3(Linux/Apache)中抛出错误。这张海报做了什么来降级?我很好奇。如果你
echo gettype($data)
flatte()
里面,结果是什么?@octopusgrabbus我相信这是因为他最初的问题缺少很多相关信息。此后,它被编辑和改进。
function flatten($data) {
    $result = array ();

    foreach ( $data as $item ) {
        if (is_array ( $item )) {
            $result [] = array_filter ( $item, 'notArray' );
            $result = array_merge ( $result, flatten ( $item ) );
        }
    }

    return $result;
}

$response = $_REQUEST ['xmlmsg'];
$data = GetXMLTree ( $response );


$result = flatten ( $data );



function GetXMLTree($xmldata) {
    // we want to know if an error occurs
    ini_set ( 'track_errors', '1' );

    $xmlreaderror = false;

    $parser = xml_parser_create ( 'ISO-8859-1' );

    xml_parser_set_option ( $parser, XML_OPTION_SKIP_WHITE, 1 );
    xml_parser_set_option ( $parser, XML_OPTION_CASE_FOLDING, 0 );
    if (! xml_parse_into_struct ( $parser, $xmldata, $vals, $datadex )) {
        $xmlreaderror = true;
        echo "errorrrrrrrrrs";
    }
    xml_parser_free ( $parser );

    if (! $xmlreaderror) {
        $result = array ();
        $i = 0;
        if (isset ( $vals [$i] ['attributes'] ))
            foreach ( array_keys ( $vals [$i] ['attributes'] ) as $attkey )
                $attributes [$attkey] = $vals [$i] ['attributes'] [$attkey];

        $result [$vals [$i] ['tag']] = array_merge ( ( array ) $attributes, ( array ) GetChildren ( $vals, $i, open ) );
    }

    ini_set ( 'track_errors', '0' );
    return $result;
}
function GetChildren($vals, &$i, $type) {
    if ($type == 'complete') {
        if (isset ( $vals [$i] ['value'] ))
            return ($vals [$i] ['value']);
        else
            return '';
    }

    $children = array (); // Contains node data


    /* Loop through children */
    while ( $vals [++ $i] ['type'] != 'close' ) {
        $type = $vals [$i] ['type'];
        // first check if we already have one and need to create an array
        if (isset ( $children [$vals [$i] ['tag']] )) {
            if (is_array ( $children [$vals [$i] ['tag']] )) {
                $temp = array_keys ( $children [$vals [$i] ['tag']] );
                // there is one of these things already and it is itself an array
                if (is_string ( $temp [0] )) {
                    $a = $children [$vals [$i] ['tag']];
                    unset ( $children [$vals [$i] ['tag']] );
                    $children [$vals [$i] ['tag']] [0] = $a;
                }
            } else {
                $a = $children [$vals [$i] ['tag']];
                unset ( $children [$vals [$i] ['tag']] );
                $children [$vals [$i] ['tag']] [0] = $a;
            }

            $children [$vals [$i] ['tag']] [] = GetChildren ( $vals, $i, $type );
        } else
            $children [$vals [$i] ['tag']] = GetChildren ( $vals, $i, $type );
            // I don't think I need attributes but this is how I would do them:
        if (isset ( $vals [$i] ['attributes'] )) {
            $attributes = array ();
            foreach ( array_keys ( $vals [$i] ['attributes'] ) as $attkey )
                $attributes [$attkey] = $vals [$i] ['attributes'] [$attkey];
                // now check: do we already have an array or a value?
            if (isset ( $children [$vals [$i] ['tag']] )) {
                // case where there is an attribute but no value, a complete with an attribute in other words
                if ($children [$vals [$i] ['tag']] == '') {
                    unset ( $children [$vals [$i] ['tag']] );
                    $children [$vals [$i] ['tag']] = $attributes;
                } // case where there is an array of identical items with attributes
elseif (is_array ( $children [$vals [$i] ['tag']] )) {
                    $index = count ( $children [$vals [$i] ['tag']] ) - 1;
                    // probably also have to check here whether the individual item is also an array or not or what... all a bit messy
                    if ($children [$vals [$i] ['tag']] [$index] == '') {
                        unset ( $children [$vals [$i] ['tag']] [$index] );
                        $children [$vals [$i] ['tag']] [$index] = $attributes;
                    }
                    $children [$vals [$i] ['tag']] [$index] = array_merge ( $children [$vals [$i] ['tag']] [$index], $attributes );
                } else {
                    $value = $children [$vals [$i] ['tag']];
                    unset ( $children [$vals [$i] ['tag']] );
                    $children [$vals [$i] ['tag']] ['value'] = $value;
                    $children [$vals [$i] ['tag']] = array_merge ( $children [$vals [$i] ['tag']], $attributes );
                }
            } else
                $children [$vals [$i] ['tag']] = $attributes;
        }
    }

    return $children;
}