dongyidao1461 2014-10-22 22:59
浏览 139
已采纳

php foreach多维数组递归无法正常工作?

Some kind of followup to my last question: for loop - move deeper on numeric key in multidimensional array

I have this array as input:

Array
(
  [0] => apl_struct Object
    (
      [funcname] => say
      [args] => Array
        (
          [0] => Array
            (
              [0] => apl_struct Object
                (
                  [funcname] => text
                  [args] => Array
                    (
                      [value] => hello
                    )
                )
            )
        )
    )
)

I now have 2 functions working for me. One is a func only for getting the next key/value in an associative array. None of the next(), prev(), etc. were working for me like on indexed arrays:

function getnext($array, $key) {
  $keys = array_keys($array);

  if ((false !== ($p = array_search($key, $keys))) && ($p < count($keys) - 1)) {
    return array('key' => $keys[++$p], 'value' => $array[$keys[$p]]);
  } else {return false;}
}

The next function is my executer or constructer. he creates a semi-xmlstruct for me. I tried to add recursion for skipping the numeric key. They're obviously nonsense and can be skipped.

I then want to check if all the values of the non-numeric keys are arrays or not. If it is an array it indicates arguments to be followed and output should look like: INPUT.

If not, it's either the functionname (funcname) or indeed a real value for us like "hello".

function arr2xml($array, $level = 1, $pos = 1) {
  $xml = '';

  foreach ($array as $key => $value) {
    if (is_object($value)) {$value = get_object_vars($value);}// convert object to array
      if (is_numeric($key)) {
        $xml .= arr2xml($value);
      } else {
        if (!is_array($value)) {
          switch ($key) {
            case 'funcname':
              $nextkey = getnext($array, $key);
              $xml .= str_repeat("\t", $level) . "<apl:$value>
";
              $xml .= arr2xml($nextkey['value'], $level++);
              $xml .= str_repeat("\t", $level) . "</apl:$value>
";
              break;
            case 'value':
              $xml .= str_repeat("\t", $level) . "\t$value
";
              break;
          }
        } else {
            $xml .= str_repeat("\t", $level) . "<$key pos='$pos'>
\t";
            $xml .= arr2xml($value, $level++, $pos++);
            $xml .= str_repeat("\t", $level) . "</$key>
";
        }
      }
  }

return $xml;
}

but what I am getting out of this so far is this: the function name was inserted right. it is say and text. also, in some wild circumstances, the -tag and the value are executed properly.

<apl:say>
<apl:text>
    hello
    </apl:text>
    <args pos='1'>
        hello
      </args>
    </apl:say>
    <args pos='1'>
    <apl:text>
    hello
    </apl:text>
    <args pos='1'>
        hello
      </args>
      </args>
</xml>

for me it looks like the recursion isn't really working. Am i missing something here? I've tried to rebuild it from previous mentioned post.

Also I'm wondering about the multiple output I am getting here. The tags seem to get filled right, but the actual arrangement is quite confusing for me.

I was expecting the output to look like this:

<apl:say>
  <args pos='1'>
    <apl:text>
      <args pos='1'>
      hello
      </args>
    </apl:text>
  </args>
</apl:say>

Thanks in advance

  • 写回答

2条回答 默认 最新

  • dongluan1901 2015-02-04 12:28
    关注

    after some time spent i came up with this solution for my problem:

    function apl2xml($array, $tlevel = 0) {
        $ret = '';
        $ret .= str_repeat("\t", $tlevel) . "<apl>
    ";
    
        foreach ($array as $key => $value) {
            $ret .= $this->aplstruct2xml($value, $tlevel + 1);
        }
    
        $ret .= str_repeat("\t", $tlevel) . "</apl>
    ";
        return $ret;
    }
    
    function aplstruct2xml($apl_struct, $tlevel = 0) {
        $ret = '';
    
        if ($apl_struct->funcname == 'text') {
            $ret .= str_repeat("\t", $tlevel) . "<text>
    ";
    
            $ret .= str_repeat("\t", $tlevel);
            $ret .= $apl_struct->args[0] . "
    ";
    
            $ret .= str_repeat("\t", $tlevel) . "</text>
    ";
        } else {
            $ret .= str_repeat("\t", $tlevel) . "<aplfunc:{$apl_struct->funcname}>
    ";
    
            foreach ($apl_struct->args as $key => $value) {
                $ret .= str_repeat("\t", $tlevel + 1) . "<arg pos='$key'>
    ";
                $ret .= $this->apl2xml($value, $tlevel + 2);
                $ret .= str_repeat("\t", $tlevel + 1) . "</arg>
    ";
            }
    
            $ret .= str_repeat("\t", $tlevel) . "</aplfunc:{$apl_struct->funcname}>
    ";
        }
        return $ret;
    }
    

    turns out that i did not need any recursion at all..

    the so called apl just consisted of an apl_struct which could contain more apl's/apl_struct's

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划