开发者

PHP: Need to merge/sort a multidimensional array based on one key in the dimension

开发者 https://www.devze.com 2023-01-08 17:14 出处:网络
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

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
   开发者_如何学Python         [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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消