Remove elements from an array in php

1

Good morning

According to a query in php, it generates the following arrangement:

Array
(
    [0] => Array
        (
            [Finca] => FINCA 1
            [0] => FINCA 1
            [Producto] => ALSTROEMERIA
            [1] => ALSTROEMERIA
            [year] => 2016
            [2] => 2016
            [week] => 26
            [3] => 26
            [quantity] => 91570
            [4] => 91570
            [quantity_origin] => 2096
            [5] => 2096
        )

    [1] => Array
        (
            [Finca] => FINCA 2
            [0] => FINCA 2
            [Producto] => ALSTROEMERIA
            [1] => ALSTROEMERIA
            [year] => 2016
            [2] => 2016
            [week] => 26
            [3] => 26
            [quantity] => 65350
            [4] => 65350
            [quantity_origin] => 1444
            [5] => 1444
        )
)

How do I remove the indexes that are numeric, that is, the arrangement is as follows:

Array
(
    [0] => Array
        (
            [Finca] => FINCA1
            [Producto] => ALSTROEMERIA
            [year] => 2016
            [week] => 26
            [quantity] => 91570
            [quantity_origin] => 2096
        )

    [1] => Array
        (
            [Finca] => FINCA 2
            [Producto] => ALSTROEMERIA
            [year] => 2016
            [week] => 26
            [quantity] => 65350
            [quantity_origin] => 1444
        )
)

I did it with array_unique (), but there are values of the elements 'quantity' and 'quantity_origin' that are the same and with this function it only leaves me the 'quantity'.

Thank you very much, I hope my question is understood.

    
asked by Fabian Sierra 13.07.2016 в 19:31
source

4 answers

4

With a foreach loop:

foreach ($arr as $key => $value) {      // Recorrer los elementos del array
    if (is_int($key)) {                 // Si la clave es un entero:
        unset($arr[$key]);              // Destruir la variable (elemento del array)
    }
}
    
answered by 13.07.2016 / 19:41
source
1

The problem is in the function that returned that array. You are using $variabledelresultado->fetch_array() and what you need is $variabledelresultado->fetch_assoc()

This will return an array without the numeric indexes, only the associative ones.

Removing them from the array after being loaded would only be a waste of memory.

    
answered by 21.08.2018 в 03:50
0

Originally you can directly get the fix only with the keys equal to the names of the camps if you use:

$arreglo = $stmt->fetch(PDO::FETCH_ASSOC);

To remove elements of an array use array_splice in this case it could be:

foreach( $data as $key => $value ) {
   if  (is_int( $key)  ) {
      array_splice( $data, $key, 1 );
   }
}
    
answered by 13.07.2016 в 19:55
0

For one-dimensional arrays. This guarantees that they are eliminated. It works for me, regards:

    while( count($arreglo) > 0 ) { 
        $arreglo = array_values($arreglo);          
        unset($arreglo[0]); 
    }
    
answered by 26.07.2018 в 20:59