How to go multidimensional picking up key and value?

Asked

Viewed 21 times

0

Hello I have a multidimensional array and I need to go through all levels

$items = [
   'janeiro' => [
       'alimentos' => [
           'item1' => 100,
           'item2' => 50,
           'item3' => 200,
       ],
       'limpeza' => [
           'item1' => 100,
           'item2' => 50,
           'item3' => 200,
       ],
       'higiene_pessoal' => [
           'item1' => 100,
           'item2' => 50,
           'item3' => 200,
       ],
   ],

   'fevereiro' => [
       'alimentos' => [
           'item1' => 100,
           'item2' => 50,
           'item3' => 200,
       ],
       'limpeza' => [
           'item1' => 100,
           'item2' => 50,
           'item3' => 200,
       ],
       'higiene_pessoal' => [
           'item1' => 100,
           'item2' => 50,
           'item3' => 200,
       ],
   ]
];

// percorrendo os array
foreach ($items as  $month => $data) {
   //print $month; devolve o mês     
   foreach($data[$month] as $category => $product) {
         $dataset[$category] = $product;
   }
 }

// primeiro nível quero pegar o mes, dentro de cada mês preciso percorrer todos os 3 arrays  

I went through the first and tried to get the key of the first level to go through the second, but give the error below:

Notice: Undefined index: janeiro in C:\Users\Administrador\php\Product.php on line 143
PHP Warning:  Invalid argument supplied for foreach() in C:\Users\Administrador\php\Product.php on line 143
  • 3

    foreach ($items as $month => $data), $month will be the name of the month and $data will be the respective array, so just go through $data in the second foreach, no $data[$month].

  • I need the name of each month, example: the first time will pick up the month January, within the month January goes through all arrays, then goes to February etc. When I pass the date it returns to me all arrays.

  • Try to do what @Woss said, you’ll realize you’re wrong and he’s right.

  • I’ll try to do it again.

  • 3

    And then review the body of the inside loop. As it is, it will only save the last month of each category in the new array.

  • That’s right @Woss is right, I made a silly mistake, on my date[0] was passing a position.

Show 1 more comment

1 answer

1

Considering that each item generates an array whose Dice is the month and each month generates another array whose Dice is a product and each product generates an array of items with the "final value" let’s say so, you will read the following way:

<?php
 foreach($items as $month => $monthItems){
     echo '<br>'. $month .' : '; // escreve o mês
     foreach($monthItems as $products => $productsItems ){
        echo '<br>&nbsp;&nbsp;'. $products .' : '; // escreve o produto
        foreach ( $productsItems as $itm => $value){
            echo '<br>&nbsp;&nbsp;&nbsp;&nbsp;'.$itm.':'.$value.' '; //escreve o itemX : valor
        }
     }
 }

And then we’ll have the exit below:

janeiro :
  alimentos :
    item1:100
    item2:50
    item3:200
  limpeza :
    item1:100
    item2:50
    item3:200
  higiene_pessoal :
    item1:100
    item2:50
    item3:200
fevereiro :
  alimentos :
    item1:100
    item2:50
    item3:200
  limpeza :
    item1:100
    item2:50
    item3:200
  higiene_pessoal :
    item1:100
    item2:50
    item3:200

Browser other questions tagged

You are not signed in. Login or sign up in order to post.