douhuang5623 2014-12-08 09:12
浏览 39
已采纳

如何爆炸键值对并在查询中使用它们

in my laravel project i have a db field in which user can store the parameters that to be considered for sorting and also their orders. That is, the data table "config" contains a column value like this

status-desc,priority-asc,..etc

In here, when i write the query i need to use these values as the order by pairs.

ORDER BY status desc, priority asc

I can hard code this in the query code like the above because the sorting columns can be different based on the users entry.

i already exploded two criteria separately like this

$params = explode(',', $sort_param->config_value);

so that i got $params[0] = status-desc and $params[1] = priority-asc ...

But how can i again explode these and fetch and use them in the query for order by??

what i did so far is:

$countt = count($params);
for($prm=0;$prm<$countt;$prm++){
list($k, $v) = explode('-', $params[$prm]);
}
 echo $k.' '.$v;

The echo $k and $v displays priority asc

  • 写回答

2条回答 默认 最新

  • dongluan5740 2014-12-08 09:23
    关注

    Try

    $tab = 'status-desc,priority-asc,value,string,demo';
    $params = explode(',',$tab);
    foreach($params as $v){
        $tab2[] = explode('-',$v);
    
    
    }
    $newTab = array_merge($params,$tab2);
    print_r($newTab);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错