Php 选择计数组并使用键和值输出到数组中

Php 选择计数组并使用键和值输出到数组中,php,arrays,Php,Arrays,我希望从数据库中选择number\u id=1的所有行,并将经验id分组,并将每个in的计数输出到一个数组中,其中experienceid作为键,而count作为值 +------------+------------+ | number_id | experience | +------------+------------+ | 1 | 4 | | 1 | 4 | | 1 | 1 |

我希望从数据库中选择
number\u id=1
的所有行,并将经验id分组,并将每个in的计数输出到一个数组中,其中experience
id
作为键,而
count
作为值

+------------+------------+
| number_id  | experience |
+------------+------------+
|  1         |  4         |
|  1         |  4         |
|  1         |  1         |
|  1         |  2         |
|  1         |  3         |
|  1         |  1         |
|  1         |  5         |
+------------+------------+
因此,结果应该是:

4 => 2
1 => 2
2 => 1
3 => 1
5 => 1
这就是我到目前为止所做的:

$query = "SELECT COUNT(*) AS SUM FROM comments WHERE number_id = '1' GROUP BY experience";

$result = mysqli_query($conn, $query);
$experience = array();
if ($result->num_rows>0) {
    while ($row = mysqli_fetch_assoc($result)) {
        $experience[] = $row['SUM'];
    }
}

print_r($experience);
哪个输出这个

Array ( [0] => 2 [1] => 2 [2] => 1 [3] => 1 [5] => 1 )
但我需要能够将它们的数组键更改为经验id


我怎样才能做到这一点呢?

只需选择
experience
并将其用作键即可

$query = "SELECT experience, COUNT(*) AS SUM FROM comments WHERE number_id = '1' GROUP BY experience";

$result = mysqli_query($conn, $query);
$experience = array();
if ($result->num_rows>0) {
    while ($row = mysqli_fetch_assoc($result)) {
        $experience[$row['experience']] = $row['SUM'];
    }
}

print_r($experience);

只需选择
experience
并将其用作键

$query = "SELECT experience, COUNT(*) AS SUM FROM comments WHERE number_id = '1' GROUP BY experience";

$result = mysqli_query($conn, $query);
$experience = array();
if ($result->num_rows>0) {
    while ($row = mysqli_fetch_assoc($result)) {
        $experience[$row['experience']] = $row['SUM'];
    }
}

print_r($experience);
array_列(mysqli_fetch_all($result,mysqli_ASSOC),'sum','experience')
array\u列(mysqli\u fetch\u all($result,mysqli\u ASSOC),'sum','experience')