dsaj20411 2012-10-12 06:12
浏览 29
已采纳

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

                }
  • 写回答

4条回答 默认 最新

  • dsgrs26202 2012-10-12 06:19
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀