php - 构建 JavaScript 对象 PHP 循环时缺少字符

标签 php javascript arrays json multidimensional-array

尝试使用许多嵌套循环和方法来构建此 JavaScript block ,我需要专家的帮助。我迭代所有“menuitems”,其中 menu_item_parent 0 的菜单项是顶级父级,其余的我构建较低级别。

如何将缺少的两个字符添加到这些循环中?

缺少一个逗号

,

每个之后

{'name' : '','url':'','subs':[
{'_id:':'1','title': '','img': ''}
,{'_id:':'1','title': '','img': ''}
]}

函数

    function make_menu_object(){
    $menu_name = 'sec_level';
    $menu = wp_get_nav_menu_object( $menu_name );
    $menuitems = wp_get_nav_menu_items( $menu->term_id, array( 'order' => 'DESC' ) );
    $objpart=null;

    $menuitems_count = count($menuitems);
    $master_counter = 0;

    foreach( $menuitems as $item ){

        // get page id from using menu item object id
        $id = get_post_meta( $item->ID, '_menu_item_object_id', true );
        // set up a page object to retrieve page data
        $page = get_page( $id );
        $link = get_page_link( $id );
        // item does not have a parent so menu_item_parent equals 0 (false)    

        if ( $item->menu_item_parent==0 ){
            if($master_counter<>0){$objpart.=']},';}
            $objpart .= <<<EOT

'$item->title' : {
'url':'',
'sections': [

EOT;
        }else{

        $counter=1;
        $the_star=1;



        $objpart .= <<<EOT
{'name' : '$item->title','url':'','subs':   [

EOT;
            $args = array( 'numberposts' => 4, 'offset'=> 0, 'post_type' => 'post', 'category' => $item->object_id);
            $myposts = get_posts( $args );

            $the_count = count($myposts);
            foreach( $myposts as $post ) {//subs output into js object
                setup_postdata($post);
                $the_empty_thumbnail = '/wp-content/uploads/2013/03/holder_img_menu.jpg';
                $the_thumbnail = get_the_post_thumbnail($post->ID, 'thumbnail');
                //$the_thumbnail_img = (!empty($the_thumbnail) ? the_post_thumbnail() : $the_empty_thumbnail);
                $the_post_title = addslashes($post->post_title);
                $objpart .= <<<EOT
{'_id:':'1','title': '','img': ''}

EOT;
                if($counter<$the_count){$objpart .= ',';}
                    $counter++;
                }


            if($the_star==1||$the_star==$counter){$objpart .=']}';}else{$objpart .=',';}
                $the_star++;
        }

        $master_counter++;
    }
$objpart .=']}';

return '<pre>'.$objpart.'</pre>';

}

电流输出

'Item 1' : {'url':'','sections': [
    {'name' : '','url':'','subs':[
      {'_id:':'1','title': '','img': ''}
      ,{'_id:':'1','title': '','img': ''}
      ]}

    {'name' : '','url':'','subs':   [
    {'_id:':'1','title': '','img': ''}
    ,{'_id:':'1','title': '','img': ''}
    ,{'_id:':'1','title': '','img': ''}
    ,{'_id:':'1','title': '','img': ''}
    ]}

  ]},

    'Item 2' : {
    'url':'',
    'sections': [
    {'name' : '','url':'','subs':   [
    {'_id:':'1','title': '','img': ''}
    ]}]}

所需输出

'Item 1' : {'url':'','sections': [
    {'name' : '','url':'','subs':[
      {'_id:':'1','title': '','img': ''}
      ,{'_id:':'1','title': '','img': ''}
      ]}
    ,
    {'name' : '','url':'','subs':   [
    {'_id:':'1','title': '','img': ''}
    ,{'_id:':'1','title': '','img': ''}
    ,{'_id:':'1','title': '','img': ''}
    ,{'_id:':'1','title': '','img': ''}
    ]}

  ]},

    'Item 2' : {
    'url':'',
    'sections': [
    {'name' : '','url':'','subs':   [
    {'_id:':'1','title': '','img': ''}
    ]}]}

最佳答案

正如对您的问题的评论所述,您应该在 PHP 中创建对象,然后使用 json_encode。

正如评论中提到的,代码太复杂,无法将其重写为答案。

但是为了给你指明正确的方向,一些伪代码展示了如何构建 PHP 对象:

function make_menu_object() {
    $itemsForJS = new array(); // Initialize an empty array to hold the menu items

    // Code to retrieve the menu items
    // ...

    $currTopLevelNode = null; // Because output from WP is flat list, state is needed

    foreach ( $menuitems as $item ) {
        // Code to retrieve data about the menu item
        // ...

        if ( $item->menu_item_parent==0 ) {
            $currTopLevelNode = new stdClass; // Initialize an empty object 
            $currTopLevelNode->url = ''; // Or something more interesting... 
            $currTopLevelNode->sections = array(); // Empty holder for the sections to come
            $itemsForJS[$item->title] = $currTopLevelNode; 
        } else {
            $section = new stdClass; 
            $section->name = ''; // Or something more interesting
            $section->url = ''; // Or maybe $item->url? 
            $section->subs = array(); // Empty array to hold data for the subs

            // Code to retrieve post
            // ... 

            foreach ($mypost as $post) {
                $sub = new stdClass; // Empty object to hold data for the page

                // Code to retrieve data about the post
                // ...

                $sub->id = '1'; // Or maybe $post->post_id? 
                $sub->title = ''; // Maybe $post->post_title? 
                $sub->url = ''; // Maybe $post->post_url? 

                $section->subs[] = $sub; // Adding the post to the section
            }

            $currTopLevelNode->sections[] = $section; // Adding the section to the item
        }
    }
    return $itemsForJS; // This is an associative array, not string as in your code
}

使用此函数类似于:

$menuObject = make_menu_object(); 
echo '<pre>'.json_encode($menuObject).'</pre>'; 

当然,这是伪代码。例如,它假设收到的第一个 $item 必须是顶级项目 - 否则 $currTopLevelNode 将为 null 并且代码将崩溃。需要进行错误检查。

关于php - 构建 JavaScript 对象 PHP 循环时缺少字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18070304/

相关文章:

javascript - Jquery:元素上的事件选择器而不是其子元素上的选择器?

javascript - 如何禁用所有自动步速预加载器事件

java - 将 double 组按升序排序

c - 将多个二维数组复制到一个一维数组的最快方法(C 语言)

php - 如何显示正确的 UTF-8 编码?

php - 在 SQL 的用户输入上组织用户输入 n 用户输入

javascript - 如何将 json 数据导入 AngularJS 模式对话框?

javascript - 我的单选按钮没有从 foreach 循环中获取值,并且表单验证返回 'field required',即使它已被选中?

php - 网址后面的用户名

c - char数组的按位运算