for loop - move deeper on numeric key in multidime

2019-09-22 04:01发布

i've managed to parse an array out of my own language. (for this: called lance project language - lpl)

so.. \lance(says(hello)) will be formatted to

Array
(
    [0] => lpl_struct
        (
            [args] => Array
                (
                    [0] => Array
                        (
                            [0] => lpl_struct
                                (
                                    [args] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [0] => lpl_struct
                                                        (
                                                            [args] => Array
                                                                (
                                                                    [0] => hello
                                                                )
                                                            [funcname] => text
                                                        )
                                                )
                                        )
                                    [funcname] => says
                                )
                        )
                )
            [funcname] => lance
        )
)

im now trying to create an xml struct out of this, here is my attempt to do so:

    function arr2xml($array) {
    $xml = '';

    if (is_array($array) || is_object($array)) {
        foreach ($array as $key => $value) {
            if (is_numeric($key)) {
                #move deeper in array to get the right NON-NUMERIC key
            }

            $xml .= '<' . $key . '>' . "\n" . $this->arr2xml($value) . '</' . $key . '>' . "\n";
        }
    } else {
        $xml = htmlspecialchars($array, ENT_QUOTES) . "\n";
    }

    return $xml;
}

what i'm getting out of it so far is:

<0>
    <args>
        <0>
            <0>
                <args>
                    <0>
                        <0>
                            <args>
                                <0>
                                hello
                                </0>
                            </args>
                            <funcname>
                            text
                            </funcname>
                        </0>
                    </0>
                </args>
                <funcname>
                says
                </funcname>
            </0>
        </0>
    </args>
    <funcname>
    lance
    </funcname>
</0>

is it possible to "skip" the numeric keys in the array?. say to.. move deeper to the next "real" array key?

our should i rethink my created array

thanks for any answer.

sry for bad formatting. im kinda new to stackoverflow. lance

1条回答
Evening l夕情丶
2楼-- · 2019-09-22 04:34

I tried to rebuild your array using the following:

$arr = array();
$arr[] = array(
    'args'=>array(
        array(
            'args'=>array(
                array(
                    'args'=>array(
                        'hello',
                    ),
                    'funcname'=>'text',
                ),
            ),
            'funcname'=>'says',
        ),
    ),
    'funcname'=>'lance',
);

I then updated your arr2xml function:

function arr2xml($array) {
    $xml = '';

    if (is_array($array) || is_object($array)) {
        foreach ($array as $key => $value) {
            if (is_numeric($key)) {
                // Skip a numeric key and recurse.
                $xml .= arr2xml($value);
            }
            else {
                $xml .= '<' . $key . '>' . "\n" . arr2xml($value) . '</' . $key . '>' . "\n";
            }
        }
    } else {
        $xml = htmlspecialchars($array, ENT_QUOTES) . "\n";
    }

    return $xml;
}

This produced the following, which is close to what you're looking for:

<args>
    <args>
        <args>
            hello
        </args>
        <funcname>
            text
        </funcname>
    </args>
    <funcname>
        says
    </funcname>
</args>
<funcname>
    lance
</funcname>

The only thing missing is the root element. You can add that manually using:

'<xml>' . arr2xml($arr) . '</xml>'

Hope this helps!

查看更多
登录 后发表回答