Php - multidimensional array using for or foreach

Asked

Viewed 84 times

0

Guys, I need help, look at the examples below:

$cores = array (‘vermelhas’,’amarelas’,’verdes’)

$ frutas = array(
                         array (‘maçã’,’morango’,’cereja’,’acerola’),
                         array (‘banana’,’melão’),
                         array (‘abacate’,’kiwi’,’pera’))

I need to display the arrays from above and then the muktidimensional arrays in list format, they should be displayed as the example below, using for or foreach:

Vermelhas
• maça
• morango
• cereja
• acerola

Amarelas
• banana
• melão

Verdes
• abacate
• kiwi
• pera
  • What is the connection of the first array with the second? Because if it is in order, the "green" will be "banana and melon".

2 answers

0

Good morning, follow an example in Pastebin: https://pastebin.com/5rcmxwPq

Each index of the array $colors has a list of fruits, which will be displayed in list format .

in the foreach php, the format is as follows:: foreach( $lists the $Indice => $values).

That way, the name of the core will be the $Indice, and the fruits will be in $values, needing another foreach on $values to display each value in the fruit list.

  • Hi, good morning! I need the one-dimensional array to be separated from the multidimensional one. Because that’s just an example, in the original code, in place of the colors has a phrase.

  • In this case we would have this format : https://pastebin.com/BR59aC46 .

  • 1

    Thank you, I’ll test it as soon as I get home.

-1

Sorry, reversed, the correct is:

$cores =array (‘vermelhas’,’amarelas’,’verdes’)
  • Use the edit link in your question to add other information. The Post Answer button should only be used to complete answers to the question. - Of Revision

  • I’m on my cell phone and I hadn’t seen it, thank you.

Browser other questions tagged

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