ok Posted December 3, 2008 Share Posted December 3, 2008 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... Quote Link to comment Share on other sites More sharing options...
balistic Posted December 3, 2008 Share Posted December 3, 2008 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. Quote Link to comment Share on other sites More sharing options...
ok Posted December 3, 2008 Author Share Posted December 3, 2008 balistic i know that, what i want is to fill an array with array using loop because i'm using a dynamic array. Because the array values will came from DATABASE. Anyone with better answer please. Quote Link to comment Share on other sites More sharing options...
dclamp Posted December 3, 2008 Share Posted December 3, 2008 have a look at array_fill_keys() Quote Link to comment Share on other sites More sharing options...
ok Posted December 3, 2008 Author Share Posted December 3, 2008 can you show me how to implement that?, using my example above? Quote Link to comment Share on other sites More sharing options...
dclamp Posted December 3, 2008 Share Posted December 3, 2008 Did you even look at the link i posted? It explains it very clearly. $names = array(); // blah blah blah $fruits = array(); // blah bla $arrayfill = array_fill_keys($names, $fruits) couldnt get any harder than that. Quote Link to comment Share on other sites More sharing options...
balistic Posted December 3, 2008 Share Posted December 3, 2008 <?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>"; ?> Quote Link to comment Share on other sites More sharing options...
ok Posted December 3, 2008 Author Share Posted December 3, 2008 dclamp thank you. balistic thank you also but you need to improve your coding make it shorter just like dclamp Quote Link to comment Share on other sites More sharing options...
balistic Posted December 3, 2008 Share Posted December 3, 2008 they both server different objectives, maybe if you were a bit clearer on what you were after... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.