Skip to content
Advertisement

PHP Array format with numbers

i have a big trouble with this code, i need format an array to use in different select, i have an array structure same of this:

array (size=6)
  0 => 
    array (size=9)
      0 => string 'Dorado' (length=6)
      1 => string '32GB' (length=4)
      2 => string 'Plastico' (length=8)
      'vlr' => string '40000' (length=5)
      'pcost' => string '0' (length=1)
      'pcomp' => string '0' (length=1)
      'sede' => 
        array (size=1)
          9 => string '0' (length=1)
      'ptc' => 
        array (size=2)
          12 => string '0' (length=1)
          11 => string '0' (length=1)
      's' => string '' (length=0)
  1 => 
    array (size=9)
      0 => string 'Dorado' (length=6)
      1 => string '32GB' (length=4)
      2 => string 'Madera' (length=6)
      'vlr' => string '40000' (length=5)
      'pcost' => string '0' (length=1)
      'pcomp' => string '0' (length=1)
      'sede' => 
        array (size=1)
          9 => string '0' (length=1)
      'ptc' => 
        array (size=2)
          12 => string '0' (length=1)
          11 => string '0' (length=1)
      's' => string '' (length=0)
  2 => 
    array (size=9)
      0 => string 'Dorado' (length=6)
      1 => string '64GB' (length=4)
      2 => string 'Plastico' (length=8)
      'vlr' => string '60000' (length=5)
      'pcost' => string '0' (length=1)
      'pcomp' => string '0' (length=1)
      'sede' => 
        array (size=1)
          9 => string '0' (length=1)
      'ptc' => 
        array (size=2)
          12 => string '0' (length=1)
          11 => string '0' (length=1)
      's' => string '' (length=0)
  3 => 
    array (size=9)
      0 => string 'Blanco' (length=6)
      1 => string '32GB' (length=4)
      2 => string 'Plastico' (length=8)
      'vlr' => string '40000' (length=5)
      'pcost' => string '0' (length=1)
      'pcomp' => string '0' (length=1)
      'sede' => 
        array (size=1)
          9 => string '0' (length=1)
      'ptc' => 
        array (size=2)
          12 => string '0' (length=1)
          11 => string '0' (length=1)
      's' => string '' (length=0)
  4 => 
    array (size=9)
      0 => string 'Blanco' (length=6)
      1 => string '64GB' (length=4)
      2 => string 'Madera' (length=6)
      'vlr' => string '60000' (length=5)
      'pcost' => string '0' (length=1)
      'pcomp' => string '0' (length=1)
      'sede' => 
        array (size=1)
          9 => string '0' (length=1)
      'ptc' => 
        array (size=2)
          12 => string '0' (length=1)
          11 => string '0' (length=1)
      's' => string '' (length=0)
  5 => 
    array (size=9)
      0 => string 'Verde' (length=5)
      1 => string '64GB' (length=4)
      2 => string 'Madera' (length=6)
      'vlr' => string '40000' (length=5)
      'pcost' => string '0' (length=1)
      'pcomp' => string '0' (length=1)
      'sede' => 
        array (size=1)
          9 => string '0' (length=1)
      'ptc' => 
        array (size=2)
          12 => string '0' (length=1)
          11 => string '0' (length=1)
      's' => string '' (length=0) 

I have this code for order in groups of the same name, for uses in html select control

foreach ($CaractProducto as $key => $item) {
        if (!isset($arrSelects[$item[0]])) {
            if ($NumeroCaracteristicas > 1) {
                for ($i = 1; $i < $NumeroCaracteristicas; $i++) {
                    $arrSelects[$item[0]][$item[$i]] = $item[$i];
                }
            } else {
                $arrSelects[$item[0]] = $item[0];
            }
        } else {
            if (!in_array($item[0], $arrSelects[$item[0]], true)) {
                if ($NumeroCaracteristicas > 1) {
                    for ($i = 1; $i < $NumeroCaracteristicas; $i++) {
                        $arrSelects[$item[0]][$item[$i]] = $item[$i];
                    }
                } else {
                    $arrSelects[$item[0]] = $item[0];
                }
            }
        }
    }

i need get values similar like this:

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

I need this format

But i get this code:

array (size=3)
  'Dorado' => 
    array (size=2)
      1 => 
        array (size=2)
          '32GB' => string '32GB' (length=4)
          '64GB' => string '64GB' (length=4)
      2 => 
        array (size=2)
          'Plastico' => string 'Plastico' (length=8)
          'Madera' => string 'Madera' (length=6)
  'Blanco' => 
    array (size=2)
      1 => 
        array (size=2)
          '32GB' => string '32GB' (length=4)
          '64GB' => string '64GB' (length=4)
      2 => 
        array (size=2)
          'Plastico' => string 'Plastico' (length=8)
          'Madera' => string 'Madera' (length=6)
  'Verde' => 
    array (size=2)
      1 => 
        array (size=1)
          '64GB' => string '64GB' (length=4)
      2 => 
        array (size=1)
          'Madera' => string 'Madera' (length=6)

What can i do for create other array inside whith numbers, no names, thank you for your help

Advertisement

Answer

Thanks to kmoser comment i ‘ve the answer whith similar code, if i want numerate my multidimensional array i have to put [] empty.

changing

$arrSelects[$item[0]][$item[$i]] = $item[$i];

For:

$arrSelects[$item[0]][$i][] = $item[$i];
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement