gpt4 book ai didi

php - 内联三元运算符不工作

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:03:44 26 4
gpt4 key购买 nike

出于某种原因,我的三元运算符分配不适用于数组的第二部分。有人看到我做错了什么吗?它应该只是查看永久链接字段是否有值,如果没有,则将 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;
}

编辑:

也不是说 Social Array 中应该有 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
)

)

最佳答案

你有:

'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 有任意值,$a = $b,否则$a = $c。给你:

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

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

关于php - 内联三元运算符不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8235904/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com