0

How can I merge these array together?

 Array
    (
        [0] => Array
            (
                [type] => Person
                [relevance] => 0.700000
                [count] => 300
                [text] => Chris
            )
    )
  Array
    (
        [0] => Array
            (
                [type] => Person
                [relevance] => 0.900000
                [count] => 400
                [text] => Chris
            )

        [1] => Array
            (
                [type] => Person
                [relevance] => 0.500000
                [count] => 200
                [text] => Tom
            )
    )

or array like this:

Array
        (
            [0] => Array
                (
                    [type] => Person
                    [relevance] => 0.700000
                    [count] => 300
                    [text] => Chris
                )


            [1] => Array
                (
                    [type] => Person
                    [relevance] => 0.900000
                    [count] => 400
                    [text] => Chris
                )

            [2] => Array
                (
                    [type] => Person
                    [relevance] => 0.500000
                    [count] => 200
                    [text] => Tom
                )
        )

The expected result is:

Array
(
    [0] => Array
        (
            [type] => Person
            [relevance] => 0.800000
            [count] => 700
            [text] => Chris
        )



    [1] => Array
        (
            [type] => Person
            [relevance] => 0.500000
            [count] => 200
            [text] => Tom
        )
)

[relevance] value is an average number

[count] value is an incremental number

The merging of these array should base on [text] value. How can I do this with php in a fast way?

Thanks for helping.

3 Answers 3

2

If this is the array

$array = Array(
    Array(
        'type' => 'Person',
        'relevance' => .7,
        'count' => 300,
        'text' => 'Chris'
    ),
    Array(
        'type' => 'Person',
        'relevance' => .9,
        'count' => 400,
        'text' => 'Chris'
    ),
    Array(
        'type' => 'Person',
        'relevance' => .5,
        'count' => 200,
        'text' => 'Tom'
    ),
);

then:

$tmp = Array();

foreach($array as $obj) {
    if(!isset($tmp[$obj['text']])) {
        $tmp[$obj['text']] = array_merge(Array('total_count'=>1),$obj);
        continue;
    }
    $tmp[$obj['text']]['count']     += $obj['count'];
    $tmp[$obj['text']]['relevance'] += $obj['relevance'];
    $tmp[$obj['text']]['total_count']++; // useful for average calculation
}

$result = Array();

foreach($tmp as $key=>$obj) {
    $obj['relevance'] = $obj['relevance']/$obj['total_count'];
    unset($obj['total_count']); // useless now
    $result[] = $obj;
}

print_r($result);
Sign up to request clarification or add additional context in comments.

2 Comments

I like your method of averaging better than mine, +1
Thank you... but I think that there is another faster solution :-) I'm waiting other answers.
2

Something like this maybe:

$newArray = array();

foreach ($oldArray as $item) {
    if (!array_key_exists($item['text'], $newArray)) {
        $newArray[$item['text']] = $item;
        $newArray[$item['relevance_values']] = array();

    } else {
        $newArray[$item['text']]['count'] += $item['count'];
        $newArray[$item['relevance_values']][] = $item['relevance'];
    }
}

foreach ($newArray as $item) {
    $relevance = 0;
    foreach ($item['relevance_values'] as $value) {
       $relevance += $value;
    }
    $item['relevance'] = $relevance / count($item['relevance_values']);
}

2 Comments

He did ask for the keys to be numeric on the newArray.
@JohnVanDeWeghe He could use array_values($newArray) to get an indexed array instead.
1

Try this:

function mergeOnText($array){
    $results = array();
    foreach($array as $person){
        $found = -1;
        foreach($results as $i => $res)
            if($res['text'] == $person['text']){
                $found = $i;
                break;
            }
        if($found > -1){
            $results[$found]['relevance'][] = $person['relevance'];
            $results[$found]['count'] += $person['count'];
        } else {
            $results[] = $person;
        }
    }
    foreach($results as $i => $res)
        $results[$i]['relevance'] = array_sum($res['relevance']) / count($res['relevance']);
    return $results;
}

Comments

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.