php - 按字母顺序从列表中获取值作为列表组

时间:2016-04-05 13:23:33

标签: php mysql arrays

我试图按字母顺序从MySQL数组中获取值,我希望得到如下结果:

[B] => Array  (
        [id] => 6
        [firstname] => Bon
        [lastname] => Jone
    ),
    Array  (
        [id] => 7
        [firstname] => bon
        [lastname] => doe
    )
[H] => Array
    (
        [id] => 1
        [firstname] => Hassan
        [lastname] => Ilyas
    )

[J] => Array
    (
        [id] => 5
        [firstname] => John
        [lastname] => Doe
    )

这是我尝试过的代码。

编辑

$result = mysqli_query($GLOBALS['conx'],"SELECT * FROM $users_table ORDER BY firstname ASC");
while ($row = mysqli_fetch_assoc($result)) {
    extract($row);
    $row['alphabets'] = ucfirst($firstname[0]);
    $data[] = $row;
}

但它输出如下:

[0] => Array
    (
        [id] => 6
        [firstname] => Bon
        [lastname] => Jone
    )

[1] => Array
    (
        [id] => 7
        [firstname] => bon
        [lastname] => doe
    )

[2] => Array
    (
        [id] => 1
        [firstname] => Hassan
        [lastname] => Ilyas
    )

[3] => Array
    (
        [id] => 5
        [firstname] => John
        [lastname] => Doe
    )

如何获得这样的输出:

A

亚当史密斯

艾伦史密斯

B

Bone Doe

Bone Joe

J

John Smith

....等

5 个答案:

答案 0 :(得分:1)

你的尝试不会做你想要的。要实现这一点,您必须创建一个嵌套数组,然后在每次迭代时检查 firstname 的第一个字母。检查该字母是否已作为数组中的键存在,并在数组中创建一个项目,其中一个空数组作为其值,如果不存在,则将该行数据添加到该新创建的空数组中。

$data = [];
while ($row = mysqli_fetch_assoc($result)) {
    $firstLetter = strtoupper($row["firstname"][0]); // convert to upper case so all are the same
    if (array_key_exists($firstLetter, $data) === false) $data[$firstLetter] = [];
    // now just add the row data:
    $data[$firstLetter][] = $row;
}

答案 1 :(得分:1)

ucfirst 会返回第一个字母大写的整个字符串,您需要首先抓住 substr 的第一个字母。

$data = [];

while ($row = mysqli_fetch_assoc($result)) {
    extract($row);
    $key = strtoupper($firstname[0]))
    $data[$key][] = $row;
}

要获得您想要的输出(粗略),请尝试以下方法:

foreach ($data as $letter => $rows)
{
    echo "<h1>{$letter}</h1>", PHP_EOL;

    foreach ($rows as $row)
    {
        echo "<p>{$row['firstname']} {$row['lastname']}</p>", PHP_EOL;
    }
}

答案 2 :(得分:1)

首先在MySQL查询中使用订单:... ORDER BY firstname ASC

使用PHP为字母创建数组

$Alphabetic_Array = array();

while ($row = mysqli_fetch_assoc($result)) {
    $Alphabetic_Array[$row['firstname'][0]][] = $row;
}

答案 3 :(得分:1)

首先,我建议您对查询中的记录进行排序,否则必须在对结果进行分组之前对其进行排序:

    SELECT * from tablename
  ORDER BY firstname ASC, lastname ASC

然后,您可以使用array_map对数组进行分组:

$result = array();
array_map
(
    function( $row ) use( &$result )
    {
        $result[strtoupper(substr($row['firstname'],0,1))][] = $row;
    },
    $data
);

(请注意,我们必须通过引用致电$result

现在$result是这样的数组:

Array
(
    [B] => Array
        (
            [0] => Array
                (
                    [id] => 7
                    [firstname] => bon
                    [lastname] => doe
                )
            [1] => Array
                (
                    [id] => 6
                    [firstname] => Bon
                    [lastname] => Jone
                )
        )
    [H] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [firstname] => Hassan
                    [lastname] => Ilyas
                )
        )
    [J] => Array
        (
            [0] => Array
                (
                    [id] => 5
                    [firstname] => John
                    [lastname] => Doe
                )
        )
)

如果您希望/可以使用PDO而不是mysqli_,则可以直接从MySQL查询中获得所需的结果。以这种方式设置您的查询:

    SELECT UPPER(SUBSTR(firstname,1,1)) as initial, tablename.* from tablename
  ORDER BY firstname ASC, lastname ASC

然后以这种方式获取:

$data->fetchAll( PDO::FETCH_ASSOC|PDO::FETCH_GROUP );

您的$data将包含所需的数组,无需其他处理。 PDO::FETCH_GROUP选项组按查询返回的第一列获取结果,在您的情况下是大写的名字第一个字母。

答案 4 :(得分:0)

让MYSQL做逻辑怎么样?

&#13;
&#13;
SELECT * FROM users_table, SUBSTRING(firstName, 1, 1) as initial ORDER BY firstname ASC
&#13;
&#13;
&#13;

然后你可以将每一行附加到一个数组

&#13;
&#13;
while ($row = mysqli_fetch_assoc($result)) {
    $Alphabetic_Array[$row['initial'] = $row;
}
&#13;
&#13;
&#13;

它只是伪代码,但我认为这个想法很清楚