Group an array with two values (PHP)

0

I find myself arranging an arrangement and I must order it by two of its fields in this case 'ppu' and 'sense'.

Achievement regroup it by ppu, but I can not do it for both.

Example of the arrangement

Array
(
  [id] => 1
  [num] => 1
  [ppu] => ZJ1805
  [sTs] => 126
  [sUsuario] => 126
  [sentido] => Ida
  [periodo] => FPMA
  [estrato] => B
  [uNegocio] => U1
  [cParada] => T-14-125-NS-5
  [cUsuario] => PC38
  [comuna] => PROVIDENCIA
  [eje] => MANUEL MONTT
  [desde] => AV. NUEVA PROVIDENCIA
  [hacia] => GRANADEROS
  [nParada] => Parada 5 / (M) Manuel Montt
  [zPaga] => 18:00 - 20:00
  [uValidan] => 11
  [uEvaden1] => 0
  [uEvaden2] => 0
  [uEvaden3] => 0
  [uEvaden4] => 0
)

The code I'm using for this is:

<?php

$enlace = mysqli_connect("localhost", "root", "", "db_evasion");
if (!$enlace) {
    echo "Error: No se pudo conectar a MySQL." . PHP_EOL;
    echo "error de depuración: " . mysqli_connect_errno() . PHP_EOL;
    echo "error de depuración: " . mysqli_connect_error() . PHP_EOL;
    exit;
}   

$query = "SELECT * FROM medicion";  
$resultados = mysqli_query($enlace, $query);

$newArray = array();
while ($resultado = mysqli_fetch_assoc($resultados)) {
    $newArray[] = $resultado; 
}

$result = array();
foreach ($newArray as $data) {
  $ppu = $data['ppu'];
  $sentido = $data['sentido'];
  if (isset($result[$ppu]) && isset($result[$sentido])) {
     $result[$ppu][] = $data;
  } else {
     $result[$ppu] = array($data);
  }
}
pre($result);
exit;
?>

This makes me sort by ppu, but I can not group by 2 criteria.

    
asked by Luippo 22.08.2017 в 21:34
source

1 answer

1

You can sort the data in the query directly.

<?php
$enlace = mysqli_connect("localhost", "root", "", "db_evasion");
if (!$enlace) {
    echo "Error: No se pudo conectar a MySQL." . PHP_EOL;
    echo "error de depuración: " . mysqli_connect_errno() . PHP_EOL;
    echo "error de depuración: " . mysqli_connect_error() . PHP_EOL;
    exit;
}   
// Ordenamos en la consulta 
$query = "SELECT * FROM medicion ORDER BY ppu, sentido";  
$resultados = mysqli_query($enlace, $query);

// Obtenemos todos los resultado de la consulta como array asociativo
// utilizamos mysqli_fetch_all en lugar de un while
$newArray = mysqli_fetch_all($resultados, MYSQLI_ASSOC);
pre($newArray);
exit;

Group by ppu

$agrupados = array();
foreach ($newArray as $key => $value)
{
    $agrupados[ $value['ppu'] ][ $value['sentido'] ] = $newArray[$key];
}



echo '<pre>';
print_r($agrupados);
echo '</pre>';

Example result:

Array
(
    [ZJ1805] => Array
        (
            [Ida] => Array
                (
                    [id] => 1
                    [ppu] => ZJ1805
                    [sentido] => Ida
                    [...]
                )

            [vuelta] => Array
                (
                    [id] => 2
                    [ppu] => ZJ1805
                    [sentido] => vuelta
                    [...]
                )
        )

    [ZJ1806] => Array
        (
            [Ida] => Array
                (
                    [id] => 3
                    [ppu] => ZJ1806
                    [sentido] => Ida
                    [...]
                )

            [vuelta] => Array
                (
                    [id] => 4
                    [ppu] => ZJ1806
                    [sentido] => vuelta
                    [...]
                )
        )
)
    
answered by 23.08.2017 / 00:29
source