SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Arrow how to fill array with array?

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

  2. #2
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by solidcodes View Post
    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...
    array_fill_keys perhaps? There are a great deal of functions for array. Just check the documentation on arrays.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  3. #3
    SitePoint Addict
    Join Date
    Jul 2008
    Location
    sudo rm -rf /
    Posts
    386
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You don't have values.
    adding array
    $names['john']=array('bannana' =>'yess');
    $names['jst']=array('bannana' =>'no');

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $names['john'] = array_merge$names['john'], $fruit ); 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    or:

    PHP Code:
    //$fruit & $names arrays already defined

    foreach ($names as $key => $value) {
     
    $names[$key] = $fruit;
    }
    //foreach 
    The example you pasted in your original post won't work - it will replace $names['john'] with the value of $fruit['banana'], then overwrite that with the value of $fruit['apple'], etc


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •