Skip to content
Advertisement

PHP Array for hard multilevel items

i have this array structure and i want to create other array structure with that:

array (size=7)
  0 => 
    array (size=9)
      0 => string 'Dorado' (length=6)
      1 => string '64GB' (length=4)
      2 => string 'Plastico' (length=8)
  1 => 
    array (size=9)
      0 => string 'Blanco' (length=6)
      1 => string '32GB' (length=4)
      2 => string 'Plastico' (length=8)
  2 => 
    array (size=9)
      0 => string 'Blanco' (length=6)
      1 => string '64GB' (length=4)
      2 => string 'Madera' (length=6)
  3 => 
    array (size=9)
      0 => string 'Verde' (length=5)
      1 => string '64GB' (length=4)
      2 => string 'Madera' (length=6)
  4 => 
    array (size=9)
      0 => string 'Azul' (length=4)
      1 => string '128GB' (length=5)
      2 => string 'Cristal' (length=7)
  5 => 
    array (size=9)
      0 => string 'Azul' (length=4)
      1 => string '128GB' (length=5)
      2 => string 'Plastico' (length=8)
  6 => 
    array (size=9)
      0 => string 'Azul' (length=4)
      1 => string '64GB' (length=4)
      2 => string 'Cristal' (length=7)

Each array item have a group of characteristics like:

0 => string 'Dorado' (length=6)
1 => string '64GB' (length=4)
2 => string 'Plastico' (length=8)
....
0 => string 'Azul' (length=4)
1 => string '128GB' (length=5)
2 => string 'Cristal' (length=7)

I need an array that have this values like

Azul=>64GB=>Plastico 128GB=>Cristal

if sub item repeats only write the children value

Azul=>64GB=>Cristal Only adds Cristal because 64GB exists

I have this code in php:

foreach ($arrCaracteristicas as $itemMadre => $valueMadre) {
    $NombreNodo = $valueMadre[0];
    if ($NumeroCaracteristicas > 1) {
        for ($i = 1; $i <= $NumeroCaracteristicas - 1; $i++) {
            $NombreCaract = $valueMadre[$i];
            echo "$NombreNodo: $NombreCaract <br>";
            
            array_push( $arrSelectMain[$NombreNodo], $NombreCaract);
            
            if (!isset($arrSelectMain[$NombreNodo][$NombreCaract])) {
                //$arrSelectMain[$NombreNodo]["d"] = $NombreCaract;
            }
            
        }
    }
}

Where $arrCaracteristicas have the long array, and $arrSelectMain have the first level of the array keys:

Code of first level:

foreach ($arrCaracteristicas as $item => $value) {
    $arrSelectMain[$value[0]] = $value[0];
}
asort($arrSelectMain);
$arrSelectMain = array_unique($arrSelectMain);

This is an image with structure that i want: https://ibb.co/Q8k198Y

If somebody can help me i will invite a beer in paypal 🙂

Advertisement

Answer

This should work, first separate the array into columns and then loop over the items.

$first = array_column($items, 0);
$second = array_column($items, 1);
$third = array_column($items, 2);

$output = [];

for ($i = 0; $i < count($first); $i++) {
        $output[$first[$i]][$second[$i]][] = $third[$i];
}

Have a look at the sample

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement