简体   繁体   English

如果else语句在数组内(速写版本仍然不起作用)php

[英]if else statement inside array (shorthand version still doesn't work) php

I had no idea it was not possible to use if else statement inside an array in php. 我不知道不可能在PHP数组中使用if语句。 I've searched stackoverflow and found that shorthand version should actually work fine. 我搜索了stackoverflow,发现速记版本实际上应该可以正常工作。

I tried it but still have errors and page doesn't load, my code: 我试过了,但是仍然有错误,页面无法加载,我的代码是:

query_posts (array(

                                ($prefooter_order_logic == 'xviews' ? "'v_sortby' => 'views', 'v_orderby' => 'desc'," : "")

                                'order' => 'DESC',
                                'orderby' => $prefooter_order_logic,

                                'posts_per_page' => '10',
                                'post_type' => 'post',
                                'category_name' => $prefooter_category_select

                            ));

This doesn't give me error: 这不会给我错误:

but it doesn't work... 但这行不通...

($prefooter_order_logic == 'xviews' ? "

                                    'v_sortby' => 'views',
                                    'v_orderby' => 'desc',

                                    " : "

                                    'order' => 'DESC',
                                    'orderby' => $prefooter_order_logic,

                                    "),

I've decided to stop playing around and do it easiest way: 我决定停止玩耍,并以最简单的方式做:

btw, any thoughts if this is the best way to do it? 顺便说一句,您是否认为这是最好的方法? or not? 或不?

                if ($prefooter_order_logic == 'xviews') {

                    query_posts (array(
                        'v_sortby' => 'views',
                        'v_orderby' => 'desc',
                        'posts_per_page' => '10',
                        'post_type' => 'post',
                        'category_name' => $prefooter_category_select
                    ));

                } else {

                    query_posts (array(
                        'order' => 'DESC',
                        'orderby' => $prefooter_order_logic,
                        'posts_per_page' => '10',
                        'post_type' => 'post',
                        'category_name' => $prefooter_category_select
                    ));

                }

here is the problem first that there is not ) after the ? 首先是这里的问题,之后没有) ? and , at teh end 并且,在末尾

$prefooter_order_logic == 'xviews' ? "'v_sortby' => 'views', 'v_orderby' => 'desc'," : "")

should be 应该

($prefooter_order_logic == 'xviews' )? "'v_sortby' => 'views', 'v_orderby' => 'desc'," : ""),

That will produce syntax error, unexpected T_CONSTANT_ENCAPSED_STRING because you are missing a comma at the end of this line: 这将产生syntax error, unexpected T_CONSTANT_ENCAPSED_STRING因为您在此行末尾缺少逗号:

($prefooter_order_logic == 'xviews' ? "'v_sortby' => 'views', 'v_orderby' => 'desc'," : ""), 
                                                                                           ^ here

Edit: 编辑:

Just modify the array after creating it, based on your condition: 根据您的条件,只需在创建数组后对其进行修改:

$arr = array(

    'posts_per_page' => '10',
    'post_type' => 'post',
    'category_name' => $prefooter_category_select

);

if($prefooter_order_logic == 'xviews')
{
    $arr['v_sortby'] = 'views';
    $arr['v_orderby'] = 'desc';
}
else
{
    $arr['order'] = 'DESC';
    $arr['orderby'] = $prefooter_order_logic;
}

query_posts($arr);

Alternatively, use array_merge() : 或者,使用array_merge()

$arr = array(

    'posts_per_page' => '10',
    'post_type' => 'post',
    'category_name' => $prefooter_category_select

);

$arr = $prefooter_order_logic == 'xviews' ? array_merge($arr, array('v_sortby' => 'views', 'v_orderby' => 'desc')) : array_merge($arr, array('order' => 'desc', 'orderby' => $prefooter_order_logic));

query_posts($arr);

You should specify what errors you have, and it's hard to tell what you are trying to do. 您应该指定存在的错误,并且很难说出您要尝试执行的操作。

However, you are missing a comma at the end of the line with the ternary operator: 但是,您在三进制运算符的行尾缺少逗号:

'desc'," : "")

Put a comma after that. 之后加逗号。

I guess you couldn't evaluate PHP code like this: 我想您无法像这样评估PHP代码:

"'v_sortby' => 'views', 'v_orderby' => 'desc',"

It should looks like piece of text for PHP interpreter, not part of your array, but I'm not sure. 对于PHP解释器,它看起来应该像一段文本,而不是数组的一部分,但我不确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM