1

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!

Rahul
  • 18,271
  • 7
  • 41
  • 60
Valle
  • 15
  • 5
  • Use `implode(' AND ')` to create the inner strings, and `implode(' OR ')` to create the outer strings. – Barmar Apr 10 '19 at 12:02
  • Basically you need all combination - try to do that forst and then use `implode` to create the actual query – dWinder Apr 10 '19 at 12:09
  • Yes, sorry, I didn't put my code before because I'm not even close... But I'm going to update my question. I think implode doesn't work becuase is all elements between themselves. – Valle Apr 10 '19 at 12:51
  • Here is a very simple solution to this problem: https://stackoverflow.com/a/44910370/2685833 – whyguy Apr 10 '19 at 14:23

1 Answers1

2

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.

Rahul
  • 18,271
  • 7
  • 41
  • 60