PHP:需要基于维度中的一个键合并/排序多维数组

时间:2010-07-21 03:56:10

标签: php multidimensional-array

我希望我能够解释我正在尝试做什么 - 并且真的希望有人能够提供帮助!这是我正在使用的数组

(
    [0] => Array
        (
            [city] => Middletown
            [state] => OH
            [freq] => 146.610
            [pl] => 77.0
            [call] => W8BLV
            [distance] => 0.0
            [notes] => ottel
        )

    [1] => Array
        (
            [city] => Middletown
            [state] => OH
            [freq] => 146.715
            [pl] => 
            [call] => N8COZ
            [distance] => 0.0
            [notes] => ct
        )

    [2] => Array
        (
            [city] => Middletown
            [state] => OH
            [freq] => 147.315
            [pl] => 77.0
            [call] => W8JEU
            [distance] => 0.0
            [notes] => ottel
        )

    [3] => Array
        (
            [city] => Middletown
            [state] => OH
            [freq] => 443.5370
            [pl] => 118.8
            [call] => W8MUM
            [distance] => 0.0
            [notes] => oe oe
        )

    [4] => Array
        (
            [city] => Middletown
            [state] => OH
            [freq] => 444.4750
            [pl] => 100.0
            [call] => AG8Y
            [distance] => 0.0
            [notes] => ottel
        )

    [5] => Array
        (
            [city] => Middletown
            [state] => OH
            [freq] => 444.8250
            [pl] => 77.0
            [call] => W8BLV
            [distance] => 0.0
            [notes] => oel
        )

    [6] => Array
        (
            [city] => Monroe
            [state] => OH
            [freq] => 443.0870
            [pl] => 
            [call] => KC8ECK
            [distance] => 4.3
            [notes] => oep
        )

    [7] => Array
        (
            [city] => Franklin
            [state] => OH
            [freq] => 145.290
            [pl] => 118.8
            [call] => WB8ZVL
            [distance] => 5.5
            [notes] => o(ca)r
        )

    [8] => Array
        (
            [city] => Franklin
            [state] => OH
            [freq] => 442.4250
            [pl] => 77.0
            [call] => WE8N
            [distance] => 5.5
            [notes] => Oae(ca)
        )

    [9] => Array
        (
            [city] => Franklin
            [state] => OH
            [freq] => 443.1500
            [pl] => 118.8
            [call] => WB8ZVL
            [distance] => 5.5
            [notes] => o(ca)r
        )

    [10] => Array
        (
            [city] => Springboro
            [state] => OH
            [freq] => 145.490
            [pl] => 77.0
            [call] => W8CYE
            [distance] => 8.9
            [notes] => oe
        )

)

我希望得到的输出必须是这样的:

$rpt[0] ="Middletown 146.61/77, 146.715, 147.315/77, 443.537/118.8, 444.475/100, 444.825/77";
$rpt[1] = "Monroe 443.087";
$rpt[2] = "Franklin 145.29/118.8, 442.425/77, 443.15/118.8";
$rpt[3] = "Springboro 145.49/77"

我最感兴趣的字段是city,freq,pl - 如果你看第一个数组,我已经删除了输出数组上的尾随零。 还有一些时候我可能需要使用call字段和notes字段,但是如果有人能指出我正确的方向我可能会想到 我需要的其余部分 - 呼叫字段和备注字段不会经常使用。 我已经尝试了几个不同的foreach循环,但我仍然是PHP的新手 - 我所做的并没有正常工作,而且我需要一些帮助。

提前感谢您的帮助, LeRoy,KD8BXP

2 个答案:

答案 0 :(得分:2)

以下是您的需求:

// Here $items is your input array.

/* First, sort input array by freq */

function compareByFreq($a, $b)
{
    if ($a['freq'] == $b['freq']) {
        return 0;
    }
    return ($a['freq'] < $b['freq']) ? -1 : 1;
}

uasort($items, 'compareByFreq');



/* Aggregate required data */

$result = array();
foreach($items as $item)
{
    if (isset($result[$item['city']])) {
        $result[$item['city']] .= ', '. $item['freq'];
    } else {
        $result[$item['city']] = $item['city'] . ' ' . $item['freq'];
    }
    if (! empty($item['pl'])) {
        $result[$item['city']] .= '/' . $item['pl'];
    }
}

$result = array_values($result); // get rid of assoc keys

答案 1 :(得分:0)

foreach($input_array as $item) {
    $output_array[$item['city']][] = array(
            'freq' => $item['freq'],
            'pl' => $item['pl']
        );
}

有了这个,$output_array最终会成为一个关联数组,其关键是城市,其值是freq + pl数组的列表:

Array
(
    ["Middletown"] => Array
        (
            [0] => Array
                (
                    [freq] => 146.610,
                    [pl] => 77.0
                ),
            [1] => Array
                (
                    [freq] => 146.715,
                    [pl] =>
                )
            // etc...
        )
    // etc...
)

(欢迎你 - Amber,KC9HTI

修改

运行上述代码后,您可以使用此代码获取所需的最终格式:

foreach($output_array as $city => $freq_list) {
    $freqs = array();
    foreach($freq_list as $freq) {
        $freq_str = $freq['freq'];
        if($freq['pl']) $freq_str .= "/" . $freq['pl'];
        $freqs[] = $freq_str;
    }
    $rpt[] = $city . " " . implode(", ", $freqs);
}

$rpt应该有你想要的数组。