在array-php中使用if else

时间:2011-11-07 16:16:08

标签: php arrays

是php的新手我不知道如何在php数组中使用if else。我试着做这样的事情

function column_title($item){
        $status=false;
        if($item->uTestimonials_approval =='0')
           $status=false;
         else
          $status=true;

        //Build row actions
        $actions = array(
             $status ? 'unapprove'      => sprintf('<a href="?page=%s&action=%s&id=%s">Unapprove</a>',$_REQUEST['page'],'unapprove',$item-> uTestimonials_id),:
            'approve1'      => sprintf('<a href="?page=%s&action=%s&id=%s">Approve</a>',$_REQUEST['page'],'approve',$item-> uTestimonials_id),

            'delete'    => sprintf('<a href="?page=%s&action=%s&id=%s">Delete</a>',$_REQUEST['page'],'delete',$item-> uTestimonials_id),
        );

        //Return the title contents
        return sprintf('%1$s <span style="color:silver">(id:%2$s)</span>%3$s',
            /*$1%s*/ $item-> uTestimonials_message,
            /*$2%s*/ $item-> uTestimonials_id,
            /*$3%s*/ $this->row_actions($actions)
        );
    }

但我正在解析异常

Parse error: syntax error, unexpected T_DOUBLE_ARROW

任何人都可以帮助我提前找到我做错了什么

我对于否定投票完全没有问题,但总是更好地说明为什么有人这样做可以让他/她自己改善

2 个答案:

答案 0 :(得分:5)

无法为键值执行此操作:

 $status ? 'unapprove'

尝试这样的事情:

    //Build row actions
    $ap = 'approve';
    $key = 'approve1';
    if($status){             
       $ap = 'unapprove';
       $key = 'unapprove';
    }
    $actions = array(
        $key => sprintf('<a href="?page=%s&action=%s&id=%s">'.$ap.'</a>',$_REQUEST['page'],$ap,$item-> uTestimonials_id),:
        'delete' => sprintf('<a href="?page=%s&action=%s&id=%s">Delete</a>',$_REQUEST['page'],'delete',$item-> uTestimonials_id),
    );

答案 1 :(得分:2)

你不能在数组定义中做到这一点。

相反,您可以在定义之后将这些添加到数组中:

$actions = array(
    'delete'    => sprintf('<a href="?page=%s&action=%s&id=%s">Delete</a>',$_REQUEST['page'],'delete',$item-> uTestimonials_id),
);

if ($status) {
    $actions['unapprove'] = sprintf('<a href="?page=%s&action=%s&id=%s">Unapprove</a>',$_REQUEST['page'],'unapprove',$item-> uTestimonials_id);
}
else {
    $actions['approve'] = sprintf('<a href="?page=%s&action=%s&id=%s">Approve</a>',$_REQUEST['page'],'approve',$item-> uTestimonials_id);
}