内联三元运算符不工作 [英] Inline Ternary Operator Not Working

查看:89
本文介绍了内联三元运算符不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,我的三元运算符赋值不适用于数组的第二部分。任何人都看到我做错了?它应该只是看看是否有一个值为永久链接字段,如果没有然后插入 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)
{
//将主类别设置为数组
$ 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'=> - > permalink))?$ child-> link_url:$ child-> permalink,
'link_name'=> $ child-> link_name,
);
}
}
}
}

return $ testArray;
}

EDIT: b

也不是说在社会数组里面应该有3个项目,而不是说有。我想知道它是否与该联接有关。这是我的输出:

 数组

[2] =&
[id] => 2
[name] => Roster
[children] => Array

[1] =& b $ b(
[linke_url] =>
[link_name] => Superstars


[2] =&
[linke_url] =>
[link_name] =>冠军和竞赛者


[3] => Array
$ b [linke_url] =>
[link_name] =>头衔历史





$ b b [3] => Array

[id] => 3
[name] =>事件
[children] =& (
[4] => Array

[linke_url] =>
[link_name] =>预览下一个事件


[5] =>数组

[linke_url] =>
[link_name] =>最新事件结果


[6] =>数组

[linke_url] =>
[link_name] =>事件归档


[7] =>数组

[linke_url] =>
[link_name] =>计划事件



$ b b)

[4] => array

[id] => 4
[name] => Media
[children] => Array

[8 ] =>数组

[linke_url] =>
[link_name] =>照片库






[5] => array

[id] => 5
[name] => Social




解决方案

您有:

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

code>'link_url',而不是'linke_url',因此看起来您正在尝试执行:



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



我建议您使用三元运算符的速记版本: ,其形式为: $ a = $ b?:$ c 这与 $ b 计算 true ,即如果 $ b 有任何值, $ a = $ b ,否则 $ a = $ c 。为您:

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

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


For some reason my ternary operator assignment isn't work for the second part of my array. Anyone see what I'm doing wrong? Its supposed to just see if there is a value for the permalink field and if there isn't then insert the link_url into the array.

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;
}

EDIT:

Also not that there should be 3 items inside the Social Array and its not saying there is. I'm wondering if it has something to do with that join. Here's my output:

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
    )

 )

解决方案

You have:

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

I'll assume you meant 'link_url' and not 'linke_url', so it looks like you're trying to do:

If $child->permalink is empty, set 'link_url' to $child->link_url, but, if $child->permalink isn't empty, set 'link_url' to $child->permalink.

I would recommend using the shorthand version of the ternary operator ?: which is the form: $a = $b ?: $c which is the same as if $b evaluates to true, i.e. if $b has any value, $a = $b, otherwise $a = $c. For you:

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

If $child->permalink has a value, that will be used, otherwise, the value of $child->link_url will be used. Good luck!

这篇关于内联三元运算符不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆