0

これらの配列を一緒にマージするにはどうすればよいですか?

 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
            )
    )

または次のような配列:

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
                )
        )

期待される結果は次のとおりです。

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



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

[関連性] 値は平均値です

[count] 値は増加する数値です

これらの配列のマージは、[テキスト] 値に基づく必要があります。どうすればphpでこれをすばやく行うことができますか?

助けてくれてありがとう。

4

3 に答える 3

2

このようなものかもしれません:

$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']);
}
于 2013-07-18T08:20:22.007 に答える