内爆多维php数组


implode multidimensional php array

我在php中的电子商务网站上工作。我有这样的多维php数组:-

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
        )
[1] => Array
    (
        [0] => 5
        [1] => 6
        [2] => 7
    )

)

我正在研究高级搜索选项。我有两张表,第一张是产品,另一张是产品属性。我想通过OR运算符内爆零索引数组值,通过OR条件内爆一个索引数组值然后内爆具有零索引的最终数组and第一个索引,如下所示:-

select * from tbl_product where id IN(select product_id from tbl_vehicleproductequipments where (equipmentvalue_id = 1 OR equipmentvalue_id = 2 OR equipmentvalue_id = 3 OR equipmentvalue_id = 4) AND (equipmentvalue_id = 5 OR equipmentvalue_id = 6 OR equipmentvalue_id = 7)

我试过这个代码:-

$eqpcond = "";
    if(!empty($_REQUEST["equipmentarr"])){
        foreach($_REQUEST["equipmentarr"] as $y => $equipval){
            $eqpcond = "select * from tbl_product where id IN (select product_id from tbl_vehicleproductequipments where ";
            foreach($equipval as $s => $vl){
                $equipcarr[] = " OR equipmentvalue_id = $vl";
            }
        }
        if(!empty($equipcarr)){
            $eqpcond = implode(" AND ",$equipcarr).")";
        }
    }

我得到了这样一个不正确的查询。

select * from tbl_product where id IN(select product_id from tbl_vehicleproductequipments where equipmentvalue_id = 1 AND OR equipmentvalue_id = 2 AND OR equipmentvalue_id = 3 AND OR equipmentvalue_id = 4 AND OR equipmentvalue_id = 5 AND OR equipmentvalue_id = 6 AND OR equipmentvalue_id = 7)

请帮帮我,因为我陷入了这种境地,我不知道该怎么做。如有任何帮助,我们将不胜感激。

提前感谢

试试这个。。我认为这将产生所需的查询。如果没有,请发布生成查询,并进行必要的更改。

$eqpcond = "";
        if (!empty($_REQUEST["equipmentarr"])) {
            foreach ($_REQUEST["equipmentarr"] as $y => $equipval) {
                $equipcstr = "";
                $equipcarr = array();
                $eqpcond = "select * from tbl_product where id IN (select product_id from tbl_vehicleproductequipments where ";
                foreach ($equipval as $s => $vl) {
                    $equipcstr .= " OR equipmentvalue_id = $vl";
                }
                $equipcstr = trim($equipcstr, 'OR');
                $equipcarr[] = $equipcstr;
            }
            if (!empty($equipcarr)) {
                $eqpcond = implode(" AND ", $equipcarr) . ")";
            }
        }

注意:我认为你没有有效的查询,where equipmentvalue_id IN (1,2,3,4) AND equipmentvalue_id IN (5,6,7)怎么可能?

查看:在线测试

这是你的欲望解决方案,使用一些内爆函数。。

$arr = array(
        array(1, 2, 3, 4),
        array(5, 6, 7)
    );

使用OR的子查询

$sql = 'select product_id from tbl_vehicleproductequipments where (equipmentvalue_id = '.implode(" OR equipmentvalue_id = ", $arr[0]).') AND (equipmentvalue_id = '.implode(" OR equipmentvalue_id = ", $arr[1]).")";

使用IN的子查询

$sql = 'select product_id from tbl_vehicleproductequipments where equipmentvalue_id IN ('.implode(",", $arr[0]).') AND equipmentvalue_id IN ('.implode(",", $arr[1]).')';

然后最后使用CCD_ 3。

$main_sql = select * from tbl_product where id IN($sql);