douyingmou1389 2013-08-11 04:13
浏览 39
已采纳

递归解析php

How can I parse a Nested ObjectArray or ObjectArray Tree with a recursivity function, and how can I get each node/cursor?

I'm parser it and create one organized structure, by posteriorly ... work with it.

My ObjectArray tree is like this:

 object(stdClass)[248]
   'id' => int 0
   'type' => string 'root' (length=4)
   'related_dropzone_id' => int 0
   'related_dropzone_order' => int 0
   'options' => 
    object(stdClass)[250]
   'children' => 
    object(stdClass)[249]
       '1376112098462' => 
        (stdClass)[247]
           'id' => string '1376112098462' (length=13)
           'type' => string 'section' (length=7)
           'related_dropzone_id' => int 0
           'related_dropzone_order' => int 0
           'dropzones' => 
            object(stdClass)[246]
              ...
           'options' => 
            object(stdClass)[245]
              ...
           'children' => 
            object(stdClass)[244]
              ...
       '1376112118210' => 
        object(stdClass)[252]
           'id' => string '1376112118210' (length=13)
           'type' => string 'section' (length=7)
           'related_dropzone_id' => int 0
           'related_dropzone_order' => int 1
           'dropzones' => 
            object(stdClass)[255]
              ...
           'options' => 
            object(stdClass)[253]
              ...
           'children' => 
            object(stdClass)[254]
              ...

It has children intro children that contains information valid for me, and I need to parse it.

This is my function code:

static public function get_content_html_render_LOM( $data_LOM , $handlebars_instance = '' , $template = '' ) {
            static $template_result = ''; // Save the result html always - recursion

            if ( $handlebars_instance == '' || ! ( $handlebars_instance instanceof Handlebars_Engine ) ) {
                Handlebars_Autoloader::register();
                $handlebars_instance = new Handlebars_Engine;
            }
            if ( isset ( $data_LOM->children ) )
                foreach ( $data_LOM->children as $cursor ) {
                    $template_children = self::read_data_file( SpireBuilder::$widgets_dir . $cursor->type . '/templates/front-end.php' , array() );

                    if ( isset($cursor->related_dropzone_id) &&  $cursor->related_dropzone_id == 0 ){
                        $template = $template_children ; // esto tengo que cambiarlo pues cuando llega a un nuevo nodo
                    }

                    // Render template with data
                    if ( ! isset( $cursor->children ) )
                        $template_result = $handlebars_instance->render( $template , $cursor );
                    else {
                        //dropzones = srray vacio
                        // Por cada dropzone del lom hacer un foreach
                             // temporal children = children de childre
                             // ordenado children = ordenar temportal childre

                             // por cada children de este children
                             // si dropne.'i' == children[related dropxzone id]
                              //        dropzone.'í' = templaate childre
                             // si no noop

                        $template_result = $handlebars_instance->render( $template , array( 'options' => $cursor->options , 'dropzones' => array( 'A' => $template_children ) ) );
                        //var_dump($template_result);
                    }
                    self::get_content_html_render_LOM( $cursor , $handlebars_instance , $template_result );
                }

            return $template_result;
        }
  • 写回答

1条回答 默认 最新

  • duanjia4220 2013-08-12 11:48
    关注

    Testing: $result[] = self::get_content_html_render_LOM( $cursor , $handlebars_instance , $template_result );

    save all return recursive function.

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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!