如何使用递归函数解析一个嵌套的ObjectArray
或ObjectArray Tree
,并如何获取每个节点/游标?
我正在解析它,并创建一个有组织的结构,以后会用它来工作。
我的ObjectArray Tree
是这样的:
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]
...
它有一个名为 children
的子元素,其中包含对我有效的信息,我需要解析它。
这是我的函数代码:
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;
}