for循环 - 在多维数组中的数字键上移动更深 [英] for loop - move deeper on numeric key in multidimensional array
问题描述
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结构这是我的尝试: 到目前为止,我得到的是: 是否可以跳过数组中的数字键?我们应该重新思考我创建的数组 p>感谢您的任何答案。 我尝试使用以下方法重建您的数组: 然后我更新了你的arr2xml函数: 这产生了以下内容,这与您正在寻找的内容非常接近: 缺少的唯一东西就是根元素。您可以使用以下方式手动添加: 希望这有助于您! 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 im now trying to create an xml struct out of this, here is my attempt to do so: what i'm getting out of it so far is: 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: I then updated your arr2xml function: This produced the following, which is close to what you're looking for: The only thing missing is the root element. You can add that manually using: Hope this helps! 这篇关于for循环 - 在多维数组中的数字键上移动更深的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ 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>
我们应该重新考虑我创建的数组
格式不正确。即时通讯有点新来stackoverflow。
lance
$ arr = array();
$ arg [] = array(
'args'=> array(
array($ b $ args'=> array(
array(
'args'=>数组(
'hello',
),
'funcname'=>'text',
),
),
'funcname'=>'said',
),
),
'funcname'=>'lance',
);
函数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>'
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
)
)
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;
}
<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>
$arr = array();
$arr[] = array(
'args'=>array(
array(
'args'=>array(
array(
'args'=>array(
'hello',
),
'funcname'=>'text',
),
),
'funcname'=>'says',
),
),
'funcname'=>'lance',
);
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;
}
<args>
<args>
<args>
hello
</args>
<funcname>
text
</funcname>
</args>
<funcname>
says
</funcname>
</args>
<funcname>
lance
</funcname>
'<xml>' . arr2xml($arr) . '</xml>'