在 array-php 中使用 if else

发布于 2024-12-14 09:00:41 字数 1342 浏览 0 评论 0原文

作为 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

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

我完全同意反对票,但最好指定为什么有人这样做这样这个人就可以提高自己

being new to php i am not ble to figure out how to use if else inside php array. i tried to do something like this

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

but i am getting following parsing exception

Parse error: syntax error, unexpected T_DOUBLE_ARROW

can any one help me to find what exactly i am doing wrong thanks in advance

I am perfectly fine for negative voting but its always better to specify why some one did that so that person can improve him/herself

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘虞初梦 2024-12-21 09:00:41

不能对键值执行此操作:

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

You cannot do this for a key value:

 $status ? 'unapprove'

Try something like this instead:

    //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),
    );
友谊不毕业 2024-12-21 09:00:41

您不能在数组定义内执行此操作。

相反,您可以将它们添加到定义之后的数组中,如下所示:

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

You cannot do that inside the array definition.

Instead, you could add these to the array after the definition like this:

$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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文