php-按列表组的字母顺序从数组中获取值

php-按列表组的字母顺序从数组中获取值,php,mysql,arrays,Php,Mysql,Arrays,我试图按字母顺序从MySQL数组中获取值,我希望得到如下结果: [B] => Array ( [id] => 6 [firstname] => Bon [lastname] => Jone ), Array ( [id] => 7 [firstname] => bon [lastname] => doe ) [H] => Ar

我试图按字母顺序从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
    )
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
                )
        )
)
如何获得这样的输出:

[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
    )
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
                )
        )
)
A. 亚当·斯密

艾伦·史密斯

B 骨母鹿

骨乔

J 约翰·史密斯


。。。。etc

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

$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;
}

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

$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;
}

ucfirst返回首字母大写的整个字符串,您只需要使用substrfirst获取第一个字母

$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;
    }
}
foreach($letter=>$rows形式的数据)
{
echo“{$letter}”,PHP_EOL;
foreach($行作为$行)
{
echo“{$row['firstname']}{$row['lastname']}

”,PHP_EOL; } }
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;
    }
}
foreach($letter=>$rows形式的数据)
{
echo“{$letter}”,PHP_EOL;
foreach($行作为$行)
{
echo“{$row['firstname']}{$row['lastname']}

”,PHP_EOL; } }
MySQL查询中的第一个使用顺序:。。。按名订购ASC

使用PHP创建字母表数组

$Alphabetic_Array = array();

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

MySQL查询中的第一个使用顺序:。。。按名订购ASC

使用PHP创建字母表数组

$Alphabetic_Array = array();

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

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

    SELECT * from tablename
  ORDER BY firstname ASC, lastname ASC
然后,您可以使用
array\u map
对阵列进行分组:

$result = array();
array_map
(
    function( $row ) use( &$result )
    {
        $result[strtoupper(substr($row['firstname'],0,1))][] = $row;
    },
    $data
);
(请注意,我们必须通过引用调用
$result

现在,
$result
是这样一个数组:

[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
    )
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\uuqli
,您可以直接从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 );
然后,以这种方式获取:

    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
选项按查询返回的第一列对获取的结果进行分组,在您的例子中,是大写的firstname第一个字母。

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

    SELECT * from tablename
  ORDER BY firstname ASC, lastname ASC
然后,您可以使用
array\u map
对阵列进行分组:

$result = array();
array_map
(
    function( $row ) use( &$result )
    {
        $result[strtoupper(substr($row['firstname'],0,1))][] = $row;
    },
    $data
);
(请注意,我们必须通过引用调用
$result

现在,
$result
是这样一个数组:

[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
    )
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\uuqli
,您可以直接从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 );
然后,以这种方式获取:

    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
选项按查询返回的第一列对获取的结果进行分组,在您的例子中是大写的firstname第一个字母。

让MYSQL执行逻辑怎么样


从users\u表中选择*子字符串(firstName,1,1)作为firstName ASC的初始顺序
让MYSQL执行逻辑怎么样



SELECT*FROM users\u table,SUBSTRING(firstName,1,1)作为firstName ASC的初始顺序
为什么不在mysql查询中排序?之前已经排序过了,很抱歉我没有在我的帖子
$result=mysqli\u查询($GLOBALS['conx',“SELECT*FROM$users\u table ORDER BY firstName ASC”)为什么不在mysql查询中对其排序?之前已经对其进行了排序,很抱歉,我没有在我的帖子
$result=mysqli\u查询($GLOBALS['conx'],“SELECT*FROM$users\u table ORDER BY firstname ASC”)已使用
$result=mysqli_query($GLOBALS['conx'],“按名字ASC从$users_表顺序选择*)给出一个应用于已使用的foreach循环的示例
$result=mysqli_query($GLOBALS['conx'],“按名字ASC从$users_表顺序选择*)
给出一个应用于foreach循环的示例,但您没有看到我使用索引0
$firstname[0]
只得到该字符串的第一个字母,而这
$I
是从哪里来的?有点匆忙,抱歉。好的,这是一个很好的建议,在数组输出中看起来不错。但是如何将其打印到字符串中,就像我在上面的问题示例中所展示的那样我添加了一些示例输出,您可能需要稍微调整HTML以使其看起来完全正确。但是您没有看到,通过使用索引0
$firstname[0],我只获得该字符串的第一个字母
那么$i是从哪里来的呢?我有点匆忙,抱歉。好的,这是一个很好的建议,在数组输出中看起来不错。但是如何将其打印成字符串,就像我在上面的问题示例中所显示的那样我添加了一些示例输出,您可能需要稍微调整HTML以使其看起来完全正确。检查我更新的问题,我已经使用了该结果
$result=mysqli\u query($GLOBALS['conx']),“SELECT*FROM$users\u table ORDER BY firstname