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
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.

Link to comment
Share on other sites

<?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>";
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.