array using explode [closed]

0

I have to generate an array with this syntax array( 100 => [ 68, 69, 72 ], 103 => [ 68, 69, 72 ], 205 => [ 68, 69, 70, 71, 72 ], 343 => [ 68, 69, 72, 73 ] )

The first field is equivalent to sizes and the second to the colors assigned to those sizes. I have the data stored in mysql for a value separated by commas with what I do an explode to separate them and generate an array.

the fields to consult would be sizes that is a varchar and colors that is another varchar and both store numbers separated by comma 100,101,135,120

the query I'm doing it like this:

    $consulta = "select * from 'Articulos' where 'id'= 111 ";
    $resultado = mysqli_query($Conectar, $consulta);
    $datos = mysqli_fetch_array($resultado);
$taLlas = explode(",", $datos['talla']); 

I'm not sure what the syntax would be to generate it.

Thank you very much everyone for your help.

    
asked by Killpe 23.10.2017 в 20:47
source

1 answer

1

If your column is called tallas and as you say in it, you have values separated by a comma. Suppose you want to create an array.

$consulta = "select * from 'Articulos' where 'id'= 111 ";
$resultado = mysqli_query($Conectar, $consulta);
$datos = mysqli_fetch_array($resultado);


while($datos = mysqli_fetch_array($resultado))
{
    $arrTallas[] = array($datos["id"]=>explode(",",$datos["tallas"]));
}

print_r($arrTallas);

An array like this will be created, since here: $arrTallas[] = array($datos["id"]=>explode(",",$datos["tallas"])); what was done was to create an associative array in which it contains a record for each array whose key would be the value of the column id (assuming that is what you want and that in your table there is a column that is so named) and within each id several arrays in turn obtained with explode on row tallas using comma , as a separator.

Reading that array would then be done through any PHP loop.

Array
(
    [0] => Array
        (
            [100] => Array
                (
                    [0] => 68
                    [1] => 69
                    [2] => 72

                )

        )

    [1] => Array
        (
            [205] => Array
                (
                    [0] => 68
                    [1] => 69
                    [2] => 70
                    [3] => 71
                    [4] => 72
               )

        )

)
    
answered by 23.10.2017 в 22:49