dtrj21373 2014-10-15 11:03
浏览 33
已采纳

for循环 - 在多维数组中更深入地移动数字键

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 . '>' . "
" . $this->arr2xml($value) . '</' . $key . '>' . "
";
        }
    } else {
        $xml = htmlspecialchars($array, ENT_QUOTES) . "
";
    }

    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条回答 默认 最新

  • duanjia7912 2014-10-16 12:47
    关注

    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 . '>' . "
    " . arr2xml($value) . '</' . $key . '>' . "
    ";
                }
            }
        } else {
            $xml = htmlspecialchars($array, ENT_QUOTES) . "
    ";
        }
    
        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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等