如何按数组键值对数组元素进行分组?

时间:2015-10-09 08:23:50

标签: php arrays

我有一个这样的数组输出;

    [0] => Array
    (
        [fis_id] => 16149
        [fis_urun] => 129
        [fis_tarihi] => 2015-10-05 17:57:03
        [kargo_tarih] => 2015-10-09 10:07:43
    )

[1] => Array
    (
        [fis_id] => 16428
        [fis_urun] => 128
        [fis_tarihi] => 2015-10-07 18:45:31
        [kargo_tarih] => 2015-10-09 09:21:18
    )

[2] => Array
    (
        [fis_id] => 16544
        [fis_urun] => 90
        [fis_tarihi] => 2015-10-08 18:23:05
        [kargo_tarih] => 2015-10-09 10:16:15
    )

[3] => Array
    (
        [fis_id] => 16535
        [fis_urun] => 91
        [fis_tarihi] => 2015-10-08 17:44:34
        [kargo_tarih] => 2015-10-09 09:16:39
    )

[4] => Array
    (
        [fis_id] => 16527
        [fis_urun] => 129
        [fis_tarihi] => 2015-10-08 17:09:20
        [kargo_tarih] => 2015-10-09 09:22:40
    )

[5] => Array
    (
        [fis_id] => 16529
        [fis_urun] => 90
        [fis_tarihi] => 2015-10-08 17:11:49
        [kargo_tarih] => 2015-10-09 09:19:00
    )

在这个数组中,我想将数组结果分组为;

product=>90 count(2)

现在,我想重新排序我的阵列,按 fis_urun 数组键分组?那可能吗 ?如果是我该怎么办?

谢谢。

3 个答案:

答案 0 :(得分:1)

根据doc

$ar1 = array(10, 100, 100, 0);
$ar2 = array(1, 3, 2, 4);
array_multisort($ar1, $ar2);

var_dump($ar1);
var_dump($ar2);

输出:

array(4) {
  [0]=> int(0)
  [1]=> int(10)
  [2]=> int(100)
  [3]=> int(100)
}
array(4) {
  [0]=> int(4)
  [1]=> int(1)
  [2]=> int(2)
  [3]=> int(3)
}

所以你需要做的是创建一个包含你的键的新数组,并根据新的一个顺序对你的第一个数组进行排序。
但首先,您必须重建当前阵列。

foreach($myMultiArray as $index => $values) {
    $myGroupedArray[$values['fis_urun']][] = $values
}

这将使您的数组看起来像这样:

[129] => Array
    (
    [0] => Array
        (
            [fis_id] => 16149
            [fis_urun] => 129
            [fis_tarihi] => 2015-10-05 17:57:03
            [kargo_tarih] => 2015-10-09 10:07:43
        )
    [1] => Array
        (
            [fis_id] => 16527
            [fis_urun] => 129
            [fis_tarihi] => 2015-10-08 17:09:20
            [kargo_tarih] => 2015-10-09 09:22:40
        )
    )

[128] => Array
    (
    [0] => Array
        (
            [fis_id] => 16428
            [fis_urun] => 128
            [fis_tarihi] => 2015-10-07 18:45:31
            [kargo_tarih] => 2015-10-09 09:21:18
        )
    )

[90] => Array
    (
    [0] => Array
        (
        [fis_id] => 16544
        [fis_urun] => 90
        [fis_tarihi] => 2015-10-08 18:23:05
        [kargo_tarih] => 2015-10-09 10:16:15
        )
    [1] => Array
        (
            [fis_id] => 16529
            [fis_urun] => 90
            [fis_tarihi] => 2015-10-08 17:11:49
            [kargo_tarih] => 2015-10-09 09:19:00
        )

[91] => Array
    (
    [0] => Array
        (
            [fis_id] => 16535
            [fis_urun] => 91
            [fis_tarihi] => 2015-10-08 17:44:34
            [kargo_tarih] => 2015-10-09 09:16:39
        )
    )

然后你必须对它进行排序:(使用数组多重排序或ksort,取决于您希望最终数组的第一级键,即(0,1,2,3)或(90 ,91,128,129)

foreach($myGroupedArray as $grouped_fis_urun => $values) {
    $myFis_urun[] = $grouped_fis_urun;
}
array_multisort($myGroupedArray, $myFis_urun);

应该这样做......

答案 1 :(得分:1)

这可能有效:

foreach ($your_array as $key => $val) {
  $newArr[$value['fis_urun']] = $value
}

答案 2 :(得分:1)

PHP中的groupby数组有一个最佳解决方案[最近添加的类]

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        // This example results in
        // 1 Html table with some pretty hierarchy
        // 2 "pretty" array
        // 3 plain print_r of result
        include "GroupBy.class.php";

        $dataRows = [
                [1, "Andy", "PHP", "4"],
                [1, "Andy", "C#", "6"],
                [2, "Josh", "C#", "6"],
                [2, "Josh", "ASP", "4"],
                [1, "Andy", "SQL", "6"],
                [3, "Steve", "SQL", "2"],
                [4, "self", "SQL", "30"],
                [4, "self", "Smalltalk", "5"],
                [4, "self", "C", "33"],
                [4, "self", "Swedish", "65"]
        ];
        // Group on the third column
        $res = (new GroupBy())->groupBy($dataRows, [ 2 ]);

        // Group on Years and Language
// $res = (new GroupBy())->groupBy($dataRows, [ 3, 2 ]);
        // You get an array back
        // Each row contains two arrays
        // First array: The key as an array of columns values
        // Second array: An array of all rows to this key


        // The above call [ 2 ] will result in
        // (pasted from output of this test)
        $x = [
                [
                        ["ASP"],
                        [
                                ["2", "Josh", "4"]
                        ]
                ],
                [
                        ["C"],
                        [
                                ["4", "self", "33"]
                        ]
                ],
                [
                        ["C#"],
                        [
                                ["1", "Andy", "6"],
                                ["2", "Josh", "6"]
                        ]
                ],
                [
                        ["PHP"],
                        [
                                ["1", "Andy", "4"]
                        ]
                ],
                [
                        ["SQL"],
                        [
                                ["1", "Andy", "6"],
                                ["3", "Steve", "2"],
                                ["4", "self", "30"]
                        ]
                ],
                [
                        ["Smalltalk"],
                        [
                                ["4", "self", "5"]
                        ]
                ],
                [
                        ["Swedish"],
                        [
                                ["4", "self", "65"]
                        ]
                ]
        ];
        // Usage (dummy)
        foreach ($res as $aGroup)
        {
            $groupKey = $aGroup[0];
            $groupRows = $aGroup[1];
            foreach ($groupRows as $groupRow)
            {
                // We got all columns in $groupRow
                // (And the key cols in $groupKey
            }
        }
        // Display as a HTML table

        echo "<code><table border='1'>";
        $runningLinenumber = 1;
        foreach ($res as $aGroup)
        {
            $groupKey = $aGroup[0];
            $groupRows = $aGroup[1];
            // first row. calc rowspan
            echo "<tr>";
            echo "<td>" . $runningLinenumber++ . "</td>"; // optional. but nice for user row interaction
            echo "<td rowspan='" . count($groupRows) . "' valign=\"top\">" . implode(",", $groupKey) . "</td>";
            echo "<td>" . implode("</td><td>", $groupRows[0]) . "</td>";
            echo "</tr>";
            // end first row
            for ($r = 1; $r < count($groupRows); ++$r)
            {
                $groupRow = $groupRows[$r];
                echo "<tr>";
                echo "<td>" . $runningLinenumber++ . "</td>"; // optional
                echo "<td>" . implode("</td><td>", $groupRow) . "</td>";
                echo "</tr>";
            }
            echo "</tr>";
        }
        echo "</table></code>";

// Display as php array initial (copy and paste from the output)

        echo '<pre>';
        echo "[\n";
        $keyAndData = [];
        for ($grpNr = 0; $grpNr < count($res); ++$grpNr)
        {
            $groupKey = $res[$grpNr][0];
            $keyTTY = '"' . implode('","', $groupKey) . '"';
            echo " [\n";
            echo " [" . $keyTTY . "],\n";
            echo " [\n";
            $groupRows = $res[$grpNr][1];
            for ($rowNr = 0; $rowNr < count($groupRows); ++$rowNr)
            {
                $groupRow = $groupRows[$rowNr];
                $aRow = '"' . implode('","', $groupRow) . '"';
                echo " [" . $aRow . "]";
                if ($rowNr != count($groupRows) - 1)
                    echo ",\n";
                else
                    echo "\n";
            }
            echo " ]\n";
            echo " ]";
            if ($grpNr != count($res) - 1)
                echo ",\n";
            else
                echo "\n";
        }
        echo "]\n";
        echo print_r($res, true);
        echo '</pre>';
        ?>
</body>
</html>