for循环 - 在多维数组中的数字键上移动更深 [英] for loop - move deeper on numeric key in multidimensional array

查看:158
本文介绍了for循环 - 在多维数组中的数字键上移动更深的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设法用我自己的语言解析一个数组。 (为此:称为lance项目语言 - lpl)

so .. \lance(说(你好))
将被格式化为

数组

[0] => lpl_struct

[args] =


$ b $ >数组

[0] =>数组

[0] => lpl_struct

[args] =>数组

[0] =>数组

[0] => lpl_struct

[args] =& b $ b(
[0] => hello

[funcname] => b


$ funcname => b


[funcname] => lance


我现在试着创建一个xml结构这是我的尝试:
$ b $ pre $ function arr2xml($ array){
$ xml ='' ;

if(is_array($ array)|| is_object($ array)){
foreach($ array as $ key => $ value){
if(is_numeric( $ key

$ b $ xml。='<'。 $键。 '>'。 \\\
。 $ this-> arr2xml($ value)。 '< /'。 $键。 '>'。 \\\
;
}
} else {
$ xml = htmlspecialchars($ array,ENT_QUOTES)。 \\\
;
}

return $ xml;

$ / code>

到目前为止,我得到的是:

 < 0> 
< args>
< 0>
< 0>
< args>
< 0>
< 0>
< args>
< 0>
hello
< / 0>
< / args>
< funcname>
text
< / funcname>
< / 0>
< / 0>
< / args>
< funcname>
表示
< / funcname>
< / 0>
< / 0>
< / args>
< funcname>
lance
< / funcname>
< / 0>

是否可以跳过数组中的数字键?我们应该重新思考我创建的数组



我们应该重新考虑我创建的数组

p>感谢您的任何答案。

格式不正确。即时通讯有点新来stackoverflow。
lance

解决方案

我尝试使用以下方法重建您的数组:

  $ arr = array(); 
$ arg [] = array(
'args'=> array(
array($ b $ args'=> array(
array(
'args'=>数组(
'hello',
),
'funcname'=>'text',
),
),
'funcname'=>'said',
),
),
'funcname'=>'lance',
);

然后我更新了你的arr2xml函数:

 函数arr2xml($ array){
$ xml ='';

if(is_array($ array)|| is_object($ array)){
foreach($ array as $ key => $ value){
if(is_numeric( $ key)){
//跳过数字键并递归。
$ xml。= arr2xml($ value);
}
其他{
$ xml。='<'。 $键。 '>'。 \\\
。 arr2xml($ value)。 '< /'。 $键。 '>'。 \\\
;
}
}
} else {
$ xml = htmlspecialchars($ array,ENT_QUOTES)。 \\\
;
}

return $ xml;



$ b $ p
$ b

这产生了以下内容,这与您正在寻找的内容非常接近:

 < args> 
< args>
< args>
hello
< / args>
< funcname>
text
< / funcname>
< / args>
< funcname>
表示
< / funcname>
< / args>
< funcname>
lance
< / funcname>

缺少的唯一东西就是根元素。您可以使用以下方式手动添加:

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

希望这有助于您!


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

解决方案

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!

这篇关于for循环 - 在多维数组中的数字键上移动更深的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆