简体   繁体   中英

PHP array multidimensional cross all elements

I need to create a sql sentence from a dinamic structure. The structure come from a multidimensional array. It is more difficult explain that show it, so I show 3 examples:

Example1: If I have this array:

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)

Example2: If I have this array:

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)

Example3: If I have this array:

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)

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_shift — Shift an element off the beginning of an array

Demo .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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