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... Link to comment https://forums.phpfreaks.com/topic/135278-how-to-fill-array-with-array/ 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. Link to comment https://forums.phpfreaks.com/topic/135278-how-to-fill-array-with-array/#findComment-704602 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. Link to comment https://forums.phpfreaks.com/topic/135278-how-to-fill-array-with-array/#findComment-704607 Share on other sites More sharing options...
dclamp Posted December 3, 2008 Share Posted December 3, 2008 have a look at array_fill_keys() Link to comment https://forums.phpfreaks.com/topic/135278-how-to-fill-array-with-array/#findComment-704613 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? Link to comment https://forums.phpfreaks.com/topic/135278-how-to-fill-array-with-array/#findComment-704618 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. Link to comment https://forums.phpfreaks.com/topic/135278-how-to-fill-array-with-array/#findComment-704621 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>"; ?> Link to comment https://forums.phpfreaks.com/topic/135278-how-to-fill-array-with-array/#findComment-704623 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 Link to comment https://forums.phpfreaks.com/topic/135278-how-to-fill-array-with-array/#findComment-704631 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... Link to comment https://forums.phpfreaks.com/topic/135278-how-to-fill-array-with-array/#findComment-704640 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.