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
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]
.– Woss
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.
– Valdir_Silva
Try to do what @Woss said, you’ll realize you’re wrong and he’s right.
– bfavaretto
I’ll try to do it again.
– Valdir_Silva
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.
– bfavaretto
That’s right @Woss is right, I made a silly mistake, on my date[0] was passing a position.
– Valdir_Silva