Array comparison

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • friglob

    Array comparison

    Lets say i have these two arrays (PHP 4.3.3)

    $arr_1 = array( [left] =22,
    [up] =4,
    [right] =10
    );

    $arr_2 = array( [bottom] =13,
    [up] =6,
    [right] =12
    );

    I need to get the new array which would look like this:

    $arr = array( [left] =22,
    [up] =10,
    [right] =22,
    [bottom] =13
    );

    the thing is that i should put two arrays into one by this rules:
    for same keys create one key with value of all values of that key
    for different keys just create their key with their value

    how to do it?

    thank you very much
  • Janwillem Borleffs

    #2
    Re: Array comparison

    friglob wrote:
    $arr = array( [left] =22,
    [up] =10,
    [right] =22,
    [bottom] =13
    );
    >
    function callback(&$item ) {
    if (is_array($item )) $item = array_sum($item );
    }
    $arr_3 = array_merge_rec ursive($arr_1, $arr_2);
    array_walk($arr _3, 'callback');

    print_r($arr_3) ;


    JW


    Comment

    Working...