内联三元运算符不工作

时间:2011-11-23 00:36:48

标签: php codeigniter ternary-operator

出于某种原因,我的三元运算符赋值对于数组的第二部分不起作用。有谁看到我做错了什么?它应该只是查看永久链接字段是否有值,如果没有,则将link_url插入数组中。

function getSiteMap()
{
    $this->db->select('site_menu_structures_links.id, site_menu_structures_links.link_name');
    $this->db->from('site_menu_structures_links');
    $this->db->where('site_menu_structures_links.status_id', 1); 
    $this->db->where('site_menu_structures_links.is_category', 'Yes'); 
    $this->db->order_by('site_menu_structures_links.sort_order'); 
    $catQuery = $this->db->get();

    if ($catQuery->num_rows())
    {
        foreach ($catQuery->result() as $cats)
        {
            // Set the Main Category into the array
            $testArray[$cats->id] = array(
                'id'   => $cats->id,
                'name' =>$cats->link_name
            );

            $this->db->select('site_content_pages.permalink, site_menu_structures_links_children.id, site_menu_structures_links_children.link_url, site_menu_structures_links_children.link_name');
            $this->db->from('site_menu_structures_links_children');
            $this->db->join('site_content_pages', 'site_content_pages.id = site_menu_structures_links_children.site_content_pages_id');
            $this->db->where('site_menu_structures_links_id', $cats->id); 
            $this->db->where('site_menu_structures_links_children.status_id', 1);  
            $this->db->order_by('site_menu_structures_links_children.sort_order'); 
            $childrenQuery = $this->db->get();



            if ($childrenQuery->num_rows())
            {
                foreach ($childrenQuery->result() as $child)
                {
                    $testArray[$cats->id]['children'][$child->id] = array(
                        'linke_url' => (empty($child->permalink)) ? $child->link_url : $child->permalink,
                        'link_name' => $child->link_name,
                    );
                }
            }
        }
    }

    return $testArray;
}

修改

也不是说社交阵列中应该有3个项目而且它没有说。我想知道它是否与该连接有关。这是我的输出:

Array
(
[2] => Array
    (
        [id] => 2
        [name] => Roster
        [children] => Array
            (
                [1] => Array
                    (
                        [linke_url] => 
                        [link_name] => Superstars
                    )

                [2] => Array
                    (
                        [linke_url] => 
                        [link_name] => Champions and Contenders
                    )

                [3] => Array
                    (
                        [linke_url] => 
                        [link_name] => Title History
                    )

            )

    )

[3] => Array
    (
        [id] => 3
        [name] => Events
        [children] => Array
            (
                [4] => Array
                    (
                        [linke_url] => 
                        [link_name] => Preview Next Event
                    )

                [5] => Array
                    (
                        [linke_url] => 
                        [link_name] => Latest Event Results
                    )

                [6] => Array
                    (
                        [linke_url] => 
                        [link_name] => Event Archives
                    )

                [7] => Array
                    (
                        [linke_url] => 
                        [link_name] => Schedule An Event
                    )

            )

    )

[4] => Array
    (
        [id] => 4
        [name] => Media
        [children] => Array
            (
                [8] => Array
                    (
                        [linke_url] => 
                        [link_name] => Photo Gallery
                    )

            )

    )

[5] => Array
    (
        [id] => 5
        [name] => Social
    )

 )

1 个答案:

答案 0 :(得分:10)

你有:

'linke_url' => (empty($child->permalink)) ? $child->link_url : $child->permalink,

我假设您的意思是'link_url'而不是'linke_url',所以看起来您正在尝试这样做:

如果$child->permalink为空,请将'link_url'设为$child->link_url,但如果$child->permalink不为空,请将'link_url'设为$child->permalink

我建议使用ternary operator ?:的简写版本,其形式为:$a = $b ?: $c,与$b评估为true时相同,即$b 1}}具有任何值$a = $b,否则为$a = $c。对你:

'link_url' => $child->permalink ?: $child->link_url

如果$child->permalink有值,则会使用该值,否则将使用$child->link_url的值。祝你好运!