Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Array help

  1. #1
    New Coder
    Join Date
    Nov 2006
    Location
    INDIA
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Array help

    Hi

    I have 2 arrays of the following structures

    $array1[]=array(
    'ID' => 1,
    'Name' => "SPP"
    );
    $array1[]=array(
    'ID' => 2,
    'Name' => "ABC"
    );

    $array2[]=array(
    'ID' => 1,
    'Name' => "SPP"
    );
    $array2[]=array(
    'ID' => 13,
    'Name' => "ABC"
    );


    I would like to merge both the arrays which results only details for Id nos 1,2 and 13 to display. How can i achieve this.


    Thanks in advance.
    -------------------------
    Shailesh Patil
    -------------------------

  • #2
    New Coder
    Join Date
    Nov 2006
    Location
    INDIA
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Any suggestion?
    -------------------------
    Shailesh Patil
    -------------------------

  • #3
    New Coder
    Join Date
    Mar 2008
    Location
    Somerset, England
    Posts
    93
    Thanks
    0
    Thanked 10 Times in 10 Posts
    This Works

    PHP Code:
    <?php
    foreach($array1 as $key => $val) {
        foreach(
    $array2 as $key2 => $val2) {
            if(
    $val['ID'] == $val2['ID']) {
                unset(
    $array2[$key2]);
            }
        }
    }

    $array3 array_merge($array1$array2);

    print_r($array3); // Show new array
    // Output is
    // Array ( [0] => Array ( [ID] => 1 [Name] => SPP ) [1] => Array ( [ID] => 2 [Name] => ABC ) [2] => Array ( [ID] => 13 [Name] => ABC ) ) 
    ?>

  • #4
    New Coder
    Join Date
    Nov 2006
    Location
    INDIA
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks.
    It worked.
    -------------------------
    Shailesh Patil
    -------------------------


  •  

    Posting Permissions

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