Jump to content

how to fill array with array?


ok

Recommended Posts

hi brothers

 

Let say i have array names

 

$names['john'];

$names['joey'];

$names['joe'];

$names['joel'];

$names['joseph'];

 

 

I want to fill these array names above with fruit names,

$fruit['banana'];

$fruit['apple'];

$fruit['grape'];

$fruit['guava'];

$fruit['strawberry'];

 

 

so for example,

$names['john'] = $fruit['banana'];

$names['john'] = $fruit['apple'];

$names['john'] = $fruit['grape'];

$names['john'] = $fruit['guava'];

$names['john'] = $fruit['strawberry'];

 

$names['joey'] = $fruit['banana'];

$names['joey'] = $fruit['apple'];

$names['joey'] = $fruit['grape'];

$names['joey'] = $fruit['guava'];

$names['joey'] = $fruit['strawberry'];

 

and soon...

 

but i want this to be automatic using loops (for or foreach).

 

Can you show me please...

 

Thank you...

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/135278-how-to-fill-array-with-array/
Share on other sites

Use multi-dimensional arrays?

 

$names = array( array("fruit", "banana"),
               array("fruit", "apple"),
               array("fruit"," grape") 
             ); 

 

used like, $names[0][1] which would hold the value of "banana"

 

i dont really get why you need the fruit array but ah well, you can remove it if its not needed.

<?php
$names = array( array("fruit", "banana"),
               array("fruit", "apple"),
               array("fruit","orange") 
             ); 
echo "<ol>";
for ($row = 0; $row < 3; $row++)
{
    echo "<li><b>The row number $row</b>";
    echo "<ul>";

    for ($col = 0; $col < 2; $col++)
    {
        echo "<li>".$names[$row][$col]."</li>";
    }

    echo "</ul>";
    echo "</li>";
}
echo "</ol>";
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.