Return value generated from foreach

0

Basically, I have this foreach to dynamically create the selection text:

foreach ($table['exists'] as $item) {
            switch ($i) {
                case 0:
                    echo($item.' = "'.$_POST[$item].'" AND');
                    break;

                case $len - 1:
                    echo(' '.$item.' = "'.$_POST[$item].'"');
                    break;

                default:
                    echo(' '.$item.' = "'.$_POST[$item].'" AND ');
                    break;
            }
            $i++;
        }

It basically returns this to me:

cep = "{CEP inserido}" AND rua = "Delfinópolis" AND dispo = "1"

If this array is inserted into the function (takes the 'exists' array and compares with the one inserted in the form):

$tabela = ['table'  => 'ceps',
                 'exists' => ['cep', 'rua', 'dispo'] ];

I needed to get this dynamically created value and insert into a variable to by in my function:

$existe = DBRead($table['table'], "WHERE {$minhaVarEntraAqui}");
    
asked by anonymous 21.04.2017 / 19:01

1 answer

1
$i=0;
        $len = count($table['exists']);
        foreach ($table['exists'] as $item) {
            switch ($i) {
                case 0:
                    $marray[0][$i] = $item.' = "'.$_POST[$item].'" AND ';
                    break;

                case $len - 1:
                    $marray[1][$i] = $item.' = "'.$_POST[$item].'"';
                    break;

                default:
                    $marray[2][$i] = $item.' = "'.$_POST[$item].'" AND ';
                    break;
            }
            for ($j = 0; $j < 3; $j++)
                echo $marray[$j][$i];
            $i++;
        }

I think this will solve the problem

    
21.04.2017 / 19:04