4

I have 2 array , each array has 2 attribute

first array :

Array (

[0] => Array
    (
        [uregisterDate] => 2013-04-03
        [total] => 4
    )

[1] => Array
    (
        [uregisterDate] => 2013-04-04
        [total] => 4
    )

[2] =>; Array
    (
        [uregisterDate] => 2013-04-05
        [total] => 3
    )

)

second array :

Array (

[0] => Array
    (
        [uregisterDate] => 2013-04-03
        [totalFailed] => 2
    )

[1] => Array
    (
        [uregisterDate] => 2013-04-04
        [totalFailed] => 4
    )

)

I want the final array to be like below output |( merge between two array with key uregistredDate:

Array (

[0] => Array
    (
        [uregisterDate] => 2013-04-03
        [total] => 4
        [totalFailed] => 2
    )

[1] => Array
    (
        [uregisterDate] => 2013-04-04
        [total] => 4
    [totalFailed] => 4
    )

[2] =>; Array
    (
        [uregisterDate] => 2013-04-05
        [total] => 3
    )

)

any idea , snippet code ?

2
  • Why not show us some code on what you've already tried to do. Commented Apr 9, 2013 at 10:36
  • @Yazmat want to merge two array into one array based on uregisterDate Commented Apr 9, 2013 at 10:45

4 Answers 4

2

try this code.

function combo($array1, $array2) {
    foreach($array1 as $key => $value) {
        if(isset($array2[$key]))
        $result[$key] = array_merge($value, $array2[$key]);
        else
        $result[$key] = $value;
    }
    return $result;
}
$array = combo(#your array 1#, #your array 2#);
print_r($array); // to view the output

UPDATED CODE

function combo($array1, $array2) {
    foreach($array1 as $key => $value) {
        if(isset($array2[$key])) {
        if($value['uregisterDate'] == $array2[$key]['uregisterDate'])
           {
            $result[$key] = array_merge($value, $array2[$key]);
            } 
            else
            {
                $result[$key] = $array2[$key];
                $result[rand(0,99)]= $value;
            }

        }
        else
        $result[$key] = $value;
    }
    return array_values($result);
}
$array = combo($a1, $a2);
print_r($array);
Sign up to request clarification or add additional context in comments.

1 Comment

You are matching main array keys.. not uregisterDate which is wrong.. and can't work.
1

Try this function

$array1 = array(
    array( "uregisterDate" => '2013-04-03', "total" => 4 ),
    array( "uregisterDate" => '2013-04-04', "total" => 4 ),
    array( "uregisterDate" => '2013-04-05', "total" => 3 )
   );

$array2 = array(
    array( "uregisterDate" => '2013-04-03', "totalFailed" => 2),
    array( "uregisterDate" => '2013-04-04', "totalFailed" => 3 )
   );

function merge_array_common_key($a1, $a2, $Ckey) {
    $merge = array_merge($a1,$a2);
    $keys = array();
    foreach ($merge as $key => $value) {
        if(isset($keys[$value[$Ckey]])){
            $merge[$keys[$value[$Ckey]]] += $value;
            unset($merge[$key]);
            continue;
        }
        $keys[$value[$Ckey]] = $key;
    }
    return $merge;
}

$test = merge_array_common_key($array1, $array2, 'uregisterDate');

var_dump($test);

Comments

0

Try something like:

function combine( $key, $array1, $array2 )
{
    $args = func_get_args();
    array_shift( $args );
    $return = $args[0];
    $arrays = array_slice( $args, 1 );

    foreach ( $arrays as $array ) {
        foreach ( $array as $arrayValue ) {
            $match_found = false;
            foreach ( $return as $i => $value ) {
                if ( isset( $value[$key] ) && isset( $arrayValue[$key] ) && $value[$key] == $arrayValue[$key] ) {
                    $return[$i] = array_merge( $value, $arrayValue );
                    $match_found = true;
                }
            }

            if ( !$match_found ) {
                $return[] = $arrayValue;
            }
        }
    }

    return $return;
}

$array = combine( "uregisterDate", $array1, $array2 );

Comments

-1

I think that the inbuilt function array_merge_recursive() would be gr8:

$array = array_merge_recursive($array1, $array2);

or function may be helpfull

$newarray = Array();
foreach ($arr1 as $element=>$value){
    $newarray = array_merge($arr1[$element],$arr2[$element])
}

2 Comments

but how can merge the data based on uregisterDate
Please complete your answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.