Foreach with two array

0

I have two arrays that come out of two select multiple that I want to combine to store them in mysql

I am using a foreach to go through one of them but I would like to know if there is the option to go through both and store them, so far this is the code I use.

foreach($_POST["dato"] as $Dato) { 
    foreach($_POST["dato2"] as $Dato2) { 
    $INuevo = Insertar_Datos("Codigos" ,"'que','idcosa','1','2'" , "'test','$Id_Numero','".$Dato."','".$Dato2."' ");
}
}

The case is that I want to loop the first read a data of the first array and then run the second inserting data from the second with a value of the first (with this in principle I have no problem once you know what would be the variables that I have to pass to the query). For example, suppose the data is first and last:

name = 1

last name = 1 of name 1

last name = 2 of the name 1

surname = 3 of the name 1

name = 2

last name = 1 of the name 2

last name = 2 of the name 2

last name = 3 of the name 2

and thus until the end of the days being able to have only two surnames or one single.

Can I or do I have to do another foreach inside the foreach? Maybe a for inside the first foreach is better?

Inside the function Insert_Data I have this:

function Insertar_Datos() { global $Conectar;
    $Parametros = func_get_args();
            $InDatos = "INSERT INTO '".$Parametros[0]."' (".$Parametros[1].") VALUES (".$Parametros[2].");";
            $RDatos = mysqli_query($Conectar, $InDatos);
            if (!$RDatos) { http_response_code(500); print(mysqli_error($Conectar)); } else { http_response_code(200); echo "ok"; }
     return $RDatos;
}

and the multiple selections are something like this:

<select name="dato[]" multiple="multiple">
    <option value="1">Numero 1</option>
    <option value="2">Numero 2</option>
    <option value="3">Numero 3</option>
</select>

<select name="dato2[]" multiple="multiple">
    <option value="1">Numero 1</option>
    <option value="2">Numero 2</option>
    <option value="3">Numero 3</option>
</select>

The structure of the table would be something like this:

+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(10)     | NO   | PRI | NULL    |       |
| que      | varchar(50) | YES  |     | NULL    |       |
| idcosa   | int(10)     | YES  |     | NULL    |       |
| 1        | varchar(200)| YES  |     | NULL    |       |
| 2        | varchar(200)| YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+

idcosa I bring it from another table and it is a number to identify where it comes from, but that number keeps it well.

The fact is that for some reason as data2 has more than one value does not work and I do not know what I have wrong: _ (

    
asked by Killpe 28.12.2016 в 19:19
source

2 answers

2

To calculate the producto cartesiano of both fixes you could use the following function:

function cartesianProduct($sets)
{
    $cartesian = array();
    foreach ($sets as $key => $set) {

        // Si un grupo esta vació no afecta el producto cartesiano
        if (empty($set)) {
            continue;
        }

        // Si esta vacio agregamos el primer grupo
        if (empty($cartesian)) {
            $cartesian[] = array();
        }

        $subset = array();
        foreach ($cartesian as $product) {
            foreach($set as $value) {
                $product[$key] = $value;
                $subset[] = $product;
            }
        }
        $cartesian = $subset;
    }
    return $cartesian;
}

Demo

Your code could result in the following:

$set = cartesianProduct(array('dato'=> $_POST['dato'], 'dato2'=> $_POST['dato2']));

foreach($set as $data) {
    $INuevo = Insertar_Datos("Codigos" ,"'que','idcosa','1','2'" , "'test','$Id_Numero','".$data['dato']."','".$data['dato2']."' ");
}
    
answered by 29.12.2016 / 12:47
source
0

To which I can understand you have: $_POST['datos'] and $_POST['datos2'] and you want to combine them to go through them. You could combine them with array_merge() : link

$array = array_merge($_POST['datos'], $_POST['datos2']);

Check if it works that way.

    
answered by 28.12.2016 в 19:39