if else语句内部数组(速记版本仍然无效)php

I had no idea it was not possible to use if else statement inside an array in php. I've searched stackoverflow and found that shorthand version should actually work fine.

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

                }

That will produce syntax error, unexpected T_CONSTANT_ENCAPSED_STRING because you are missing a comma at the end of this line:

($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():

$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);

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'," : ""),

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:

"'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.