简体   繁体   English

PHP数组多维交叉所有元素

[英]PHP array multidimensional cross all elements

I need to create a sql sentence from a dinamic structure. 我需要从dinamic结构创建一个sql语句。 The structure come from a multidimensional array. 结构来自多维数组。 It is more difficult explain that show it, so I show 3 examples: 显示它更难解释,所以我展示了3个例子:

Example1: If I have this array: 例1:如果我有这个数组:

myarry Array
(
    [5] => Array
        (
            [0] => 2
            [1] => 5
        )

    [6] => Array
        (
            [0] => 11
        )
)

I need to create a string like: (2 AND 11) OR (5 AND 11) 我需要创建一个字符串,如:(2 AND 11)OR(5 AND 11)

Example2: If I have this array: 例2:如果我有这个数组:

myarry Array
(
    [5] => Array
        (
            [0] => 2
            [1] => 5
        )

    [6] => Array
        (
            [0] => 11
            [1] => 8
        )
)

I need to create a string like: (2 AND 11) OR (5 AND 11) OR (2 AND 8) OR (5 AND 8) 我需要创建一个字符串,如:(2 AND 11)OR(5 AND 11)OR(2 AND 8)OR(5 AND 8)

Example3: If I have this array: 例3:如果我有这个数组:

myarry Array
(
    [5] => Array
        (
            [0] => 2
            [1] => 5
        )

    [6] => Array
        (
            [0] => 11
        )

    [7] => Array
        (
            [0] => 70
            [1] => 71
            [2] => 72
        )
)

I need to create a string like: (2 AND 11 AND 70) OR (2 AND 11 AND 71) OR (2 AND 11 AND 72) OR (5 AND 11 AND 70) OR (5 AND 11 AND 71) OR (5 AND 11 AND 72) 我需要创建一个字符串,如:(2 AND 11和70)或(2和11和71)或(2和11和72)或(5和11和70)或(5和11和71)或(5和11和72)

And so on... The index on the array are not important. 依此类推......数组上的索引并不重要。

I have tried already: 我已经尝试过了:

foreach ($myarry as $clave => $feature){
        ${"feat_$n"} = $feature;
        $n++;
    }
$quan= count($myarry);

    foreach ($feat_0 as $feature1) {

        for ($m = 1; $m < $quan; $m++){
            $name = "feat_{$m}";
            foreach ($$name as $feature2) {
                echo "OR feature1: ".$feature1." AND feature2: ".$feature2."<br>";
            }
        }
    }

And also: 并且:

foreach ($myarry as $clave => $feature){
        ${"feat_$n"} = $feature;
        $n++;
    }
$i =  0;
    foreach ($feat_0  as $clave0 => $feature0) {
        for ($m = 1; $m < $cantidad; $m++){
            $name = "feat_{$m}";
            foreach ($$name  as $clave1 => $feature1) {

                echo "-feature0: ".$feature0." - feature1: ".$feature1." - i: ".$i." - m: ".$m."<br>";
                $i++;
                if($m == 1)
                    $indice = $feature1;
                else
                    $pena[$feature0][$indice][$i] = $feature1;
            }
            $i=0;
        }
    }

But I'm not even close to the solution :( I hope the question is clear. Any help will be welcome! 但我甚至不接近解决方案:(我希望问题很清楚。欢迎任何帮助!

Here is the custom function from source with some modification, 这是源代码中的自定义函数,有一些修改,

First I created unique combinations of all the arrays elements like a set and then I mapped them to create the required string. 首先,我创建了所有数组元素的唯一组合,如集合,然后我映射它们以创建所需的字符串。

function custom_function($myarry)
{
    if (count($myarry) == 0) {
        return array();
    }
    $a = array_shift($myarry);
    if (count($myarry) == 0) {
        $c = array(array());
    } else {
        $c = custom_function($myarry); // recursive call
    }
    $r = array();
    foreach ($a as $v) {
        foreach ($c as $p) {
            $r[] = array_merge(array($v), $p);
        }
    }
    return $r;
}
$temp   = custom_function($myarry);
$andArr = [];
array_walk($temp, function ($item, $key) use (&$andArr) {
    $andArr[] = '(' . implode(" AND ", $item) . ') ';
});
$str = implode(' OR ', $andArr);

array_walk — Apply a user supplied function to every member of an array array_walk - 将用户提供的函数应用于数组的每个成员
array_shift — Shift an element off the beginning of an array array_shift - 从数组的开头移出一个元素

Demo . 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM