SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Nov 2002
    Location
    Maryland
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need Help Merging Arrays

    Hello,
    I am trying to merge the arrays outputted from GetPersonSubPractices into one array.

    PHP Code:
    <?php
    $i 
    0;
    while(
    $i count($practices)){
        
    $sub_practices $obj->GetPersonSubPractices($practices[$i]['practice_id']);
        
    $i++;
    }
    ?>
    The output of GetPersonSubPractices is a multi-dimensional array that looks like this:

    PHP Code:
    $sub_practices = array( 

    => array('practice_id' => 4'practice_order' => 1'practice_order' => 5'practice_name' => "Bankruptcy and Workouts"'practice_url_name' => "bankruptcy-workouts"),

    => array('practice_id' => 13'practice_order' => 3'practice_order' => 5'practice_name' => "Receiverships"'practice_url_name' => "receiverships"),

    => array('practice_id' => 1'practice_order' => 4'practice_order' => 5'practice_name' => "Banking and Financial Institutions"'practice_url_name' => "banking-financial-institutions")

    ); 
    With the code that I have, how do I use array_merge() to combine all my arrays in the while loop?

    Any help really appreciated.

    Thanks.

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,058
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    I believe this is what you would want using array_merge
    PHP Code:
    <?php
    $i 
    0;
    $allPractices = array();
    while(
    $i count($practices)){
        
    array_merge($allPractices$obj->GetPersonSubPractices($practices[$i]['practice_id']));
        
    $i++;
    }
    ?>

  3. #3
    SitePoint Addict
    Join Date
    Nov 2002
    Location
    Maryland
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cpradio,
    Thanks for the response, but it didn't work. I want all the arrays of $obj->GetPersonSubPractices to be combined into one array. When I run your code $allPractices is empty. Any other ideas?

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,058
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I misread the documentation on php.net
    PHP Code:
    <?php
    $i 
    0;
    $allPractices = array();
    while(
    $i count($practices)){
        
    $allPractices array_merge($allPractices$obj->GetPersonSubPractices($practices[$i]['practice_id']));
        
    $i++;
    }
    ?>

  5. #5
    SitePoint Addict
    Join Date
    Nov 2002
    Location
    Maryland
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That worked! Thanks cpradio!

  6. #6
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    could be cleaned up a little with a foreach, but otherwise it's the easiest way i can think of.... was trying to push it around a bit with array_map and array_walk, but it's the old inside-out problem all over again.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


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
  •