SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help with array_intersect_key() ?

    Problem: see the inline comments in the following code:
    PHP Code:
    <?php
    $array1 
    = array(
        
    'key1' => array(
            
    'name' => 'Product1.1'
            
    ,'price' => 220
            
    ,'qty_in_x' => 100
        
    )
        ,
    'key2' => array(
            
    'name' => 'Product1.2'
            
    ,'price' => 120
            
    ,'qty_in_x' => 150
        
    )
        
    /* and so on... */
    );

    $array2 = array(
        
    'key11' => array(
            
    'name' => 'Product2.1'
            
    ,'price' => 50
            
    ,'qty_in_y' => 150
        
    )
        ,
    'key2' => array(
            
    'name' => 'Product2.2'
            
    ,'price' => 80
            
    ,'qty_in_y' => 180
        
    )
        
    /* and so on... */
    );

    //what i want to do is intersect the two arrays by keys and want to get the results as:
    $final_array = array(
        
    'key2' => array(
            
    'name' => 'Product1.2'
            
    ,'price' => 120
            
    ,'qty_in_x' => 150
            
    ,'qty_in_y' => 180 //Note: this should be merged from $array2
        
    )
    );

    //I tried with:
    $final_array array_intersect_key($array1$array2);
    print_r($final_array);
    /*Which Results:
    Array
    (
        [key2] => Array
            (
                [name] => Product1.2
                [price] => 120
                [qty_in_x] => 150
            )

    )
    which just gave the fields from $array1 excluding 'qty_in_y'
    */
    ?>
    Is there any way to accomplish as mentioned above?
    Thanks

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is your clue
    //Note: this should be merged
    Intersect means find the common elements of two arrays, not the unique ones.

    I'm suspecting there is either something wrong with your logic or example, though. Why would product 1 care what product 2 has in x or y?

  3. #3
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by hash View Post
    This is your clue

    Intersect means find the common elements of two arrays, not the unique ones.

    I'm suspecting there is either something wrong with your logic or example, though. Why would product 1 care what product 2 has in x or y?
    I think you didn't get my points.
    Let me explain.
    There are two arrays and i am intersecting on the basis of keys (irrelevant of values).
    So on that basis we will get an array with common key: 'key2' as:
    Code:
    Array
    (
        [key2] => Array
            (
                [name] => Product1.2
                [price] => 120
                [qty_in_x] => 150
            )
    
    )
    since the master array is $array1.
    Now look at the slave array $array2 with key: 'key2' which is given as:
    Code:
    'key2' => array(
    		'name' => 'Product2.2'
    		,'price' => 80
    		,'qty_in_y' => 180
    	)
    and i want to have this 'qty_in_y' merged in above results.
    Hope my question is clear now. I guessed so.
    Thanks

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Still doesn't make much logical sense, but how about this:
    PHP Code:
    foreach($arr1 as $k => $v) {
        
    array_merge($varray_diff_key($v$arr2[$k]));

    (after you have done the intersect)

  5. #5
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by hash View Post
    Still doesn't make much logical sense, but how about this:
    PHP Code:
    foreach($arr1 as $k => $v) {
        
    array_merge($varray_diff_key($v$arr2[$k]));

    (after you have done the intersect)
    I don't want to loop after intersection.
    I was wondering to know if there is such function which intersect with key and merge the new fields(like in our case).
    Thanks

  6. #6
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No.

  7. #7
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I deployed with simpler approach(without using array_intersect_key()):
    PHP Code:
    $final_array = array();
    foreach(
    $array1 as $key => $value) {
        if(isset(
    $array2[$key])){
            
    $final_array[$key] = array_merge($value$array2[$key]);
        }

    any other ways?

  8. #8
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That won't do what you specified. With array_merge() the second array will overwrite the first, so instead of name => 1.2 you will have name => 2.2

    But whatever ... you spec'd it right, your logic is flawless, only our answers are faulty.

  9. #9
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by hash View Post
    That won't do what you specified. With array_merge() the second array will overwrite the first, so instead of name => 1.2 you will have name => 2.2

    But whatever ... you spec'd it right, your logic is flawless, only our answers are faulty.
    Sorry my mistake.
    array_merge($value, $array2[$key]) >==> array_merge($array2[$key], $value)
    So it becomes:
    PHP Code:
    $final_array = array();
    foreach(
    $array1 as $key => $value) {
        if(isset(
    $array2[$key])){
            
    $final_array[$key] = array_merge($array2[$key], $value);
        }



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
  •