PHP:需要根据维度中的一个键对多维数组进行合并/排序

发布于 2024-09-10 13:47:15 字数 3455 浏览 1 评论 0原文

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

(
    [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"

我最感兴趣的字段是城市、频率、pl - 如果你看第一个数组,我已经删除了输出数组上的尾随零。 还有几次我可能需要使用呼叫字段和注释字段,但如果有人可以给我指出正确的方向,我可能就能弄清楚 我需要的其余部分 - 调用字段和注释字段不会经常使用。 我已经尝试了几个不同的 foreach 循环,但我仍然是 PHP 的新手 - 而且我所做的并没有正确工作,而且我现在需要一些帮助。

预先感谢您的帮助, 勒罗伊,KD8BXP

I hope I will be able to explain what I'm trying to do - and really hope someone will be able to help! Here is the array I am working with

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

)

The output I'm looking to get needs to be something like this:

$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"

The fields I am most interested in are the city, freq, pl - And if you look at the first array, I have removed the trailing zeros on the output array.
There are also a couple of times when I may need to use the call field, and the notes field, but if someone can point me in the right direction I probably will be able to figure
the rest I need - the call field and notes field would not be used often.
I have tried a couple of different foreach loops, but I am still pretty much a novice at PHP - and what I've done hasn't worked correctly, and I'm at the point were I need a little help.

Thanks in advance for the help,
LeRoy, KD8BXP

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黑凤梨 2024-09-17 13:47:15

这是您需要的:

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

Here is what you need:

// 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
终遇你 2024-09-17 13:47:15
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 应该有您想要的数组。

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

With this, $output_array will end up as an associative array whose keys are cities, and whose values are lists of freq+pl arrays:

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

(And you're welcome - Amber, KC9HTI)

Edit

Once you've run the above code, you can use this code to get the final format you wanted:

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 should then have the array you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文