如何从数组中创建查询字符串? [英] How to make query string line from array?

查看:85
本文介绍了如何从数组中创建查询字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!

我有一个数组:



Hello!
I have an array:

array(8) {
  ["type"]=>
  array(2) {
    [0]=>
    string(1) "1"
    [1]=>
    string(1) "2"
  }
  ["rooms"]=>
  array(2) {
    [0]=>
    string(1) "1"
    [1]=>
    string(1) "2"
  }
  ["currency"]=>
  array(4) {
    [0]=>
    string(1) "1"
    [1]=>
    string(1) "2"
    [2]=>
    string(1) "3"
    [3]=>
    string(1) "4"
  }
  ["country"]=>
  array(2) {
    [0]=>
    string(1) "1"
    [1]=>
    string(1) "2"
  }
  ["city"]=>
  array(4) {
    [0]=>
    string(1) "1"
    [1]=>
    string(1) "2"
    [2]=>
    string(1) "3"
    [3]=>
    string(1) "4"
  }
  ["region"]=>
  array(4) {
    [0]=>
    string(1) "1"
    [1]=>
    string(1) "3"
    [2]=>
    string(1) "4"
    [3]=>
    string(1) "2"
  }
  ["village"]=>
  array(2) {
    [0]=>
    string(1) "1"
    [1]=>
    string(1) "2"
  }
  ["position"]=>
  array(2) {
    [0]=>
    string(1) "1"
    [1]=>
    string(1) "2"
  }
}





我想为下一个例子创建SQL查询字符串:

WHERE type = 1 OR type = 1 AND rooms = 1 OR rooms = 2 AND currency = 1 OR currentcy = 2 ... etc。



现在我试试这样:





I want to make SQL query string for next example:
WHERE type = 1 OR type = 1 AND rooms =1 OR rooms =2 AND currency = 1 OR currentcy = 2 ...etc.

Now i try such:

foreach ($this->or as $key => $value) {

                foreach ($value as $keys => $val) {
                    $this->where.=' OR '.$key.'='.$val;
                }

                $this->where.= ' AND ';
            }

            echo $this->where;









但是得到丑陋的代码和结果查询:



OR type = 1 OR type = 2 AND OR rooms = 1 OR rooms = 2 AND OR货币= 1或货币= 2或货币= 3或货币= 4 AND OR国家= 1或国家= 2 AND OR city = 1 OR city = 2 OR city = 3 OR city = 4 AND OR region = 1 OR region = 3 OR region = 4 OR region = 2 AND OR village = 1 OR village = 2 AND OR position = 1 OR position = 2 AND



你能给吗?我喜欢美丽吗?





But get ugly code and the result query:

OR type=1 OR type=2 AND OR rooms=1 OR rooms=2 AND OR currency=1 OR currency=2 OR currency=3 OR currency=4 AND OR country=1 OR country=2 AND OR city=1 OR city=2 OR city=3 OR city=4 AND OR region=1 OR region=3 OR region=4 OR region=2 AND OR village=1 OR village=2 AND OR position=1 OR position=2 AND

Can you give me idia to make it beauty?

推荐答案

this->或 as
this->or as


key = >


value){

foreach


这篇关于如何从数组中创建查询字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆