本文介绍了如何解决:array_push 似乎没有保存附加值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从数据库中为电报机器人加载用户信息.我获取了 chatid、汽车号码和计时器值.

I'm loading user information from a database for a telegram bot. I fetch the chatid, car number, and a timer value.

创建一个数组来保存不同的汽车及其用户.

An array is created to hold the different cars with their users.

随后,我检查每个数据库结果/用户的汽车是否存在于数组中.如果不是,则将新数组推送到该数组中.如果存在汽车,则将当前用户作为数组添加到用户数组中.

Subsequently, I check for each database result/user if their car is present in the array. If not, a new array is pushed into the array. If the car is present, the current user is added to as an array into the array of users.

然而,这似乎不起作用.注意:为了检查数组中是否存在该值,我使用了一个单独的函数,因为 in_array 不适用于 multi dim.此功能已经过测试并有效.

However, this doesn't seem to work.Note: to check if the value exists in the array, I'm using a separate function, since in_array doesn't work with multi dim. This function is tested and works.

foreach 循环中的第一个 echo 输出一个数组,其中显示了所有用户.退出循环时,第二次打印,只显示原始用户.

The first echo within the foreach loop outputs an array in which all users are shown. When out of the loop, the second print, only shows the original user.

数据库中目前有两个用户,注册了同一辆车.

In the database are currently two users present, registered with the same car.

$sql = 'SELECT chatid, car, timer FROM telegram_users WHERE car IS NOT NULL';
    $result = $connection->query($sql);
    $array = [];
    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            if ($row['timer'] != NULL) {
                if (!in_array_r($row['car'], $array)) {
                    array_push($array, ['car' => $row['car'], 'users' => [['user' => $row['chatid'], 'timer' => $row['timer']]], 'hunt' => '']);
                } else {
                    foreach ($array as $element) {
                        if ($element['car'] == $row['car']) {
                            array_push($element['users'], ['user' => $row['chatid'], 'timer' => $row['timer']]);
                        }
                        echo print_r($element, true);
                    }
                    echo print_r($array, true);
                }
            }
        }
    }

推荐答案

您在 foreach 中的代码只会将值推送到 $element 变量中.没有进入原始的 $array 变量.要修改原始 $array 变量,您有 2 个选项.

Your code in foreach only pushes the values into $element variable. Not into the original $array variable. To modify original $array variable you have 2 options.

1) 通过引用为foreach赋值

1) Assign the value in foreach by reference

foreach ($array as &$element) {
    if ($element['car'] == $row['car']) {
        array_push($element['users'], ['user' => $row['chatid'], 'timer' => $row['timer']]);
    }
    echo print_r($element, true);
}

确保添加 &在 foreach 中的 $element 前面.这将允许您编辑原始的 $array.有关 foreach 和参考的更多信息请参阅 php 手册.

Make sure to add the & in front of $element in foreach. That will allow you to edit the original $array.For more info about foreach and references see php manual.

2) 您可以使用索引并直接推送到 $array 变量

2) You can use indexes and push directly to $array variable

foreach ($array as $index => $element) {
    if ($element['car'] == $row['car']) {
        array_push($array[$index]['users'], ['user' => $row['chatid'], 'timer' => $row['timer']]);
    }
}

优化建议

您可以使用 $row['car'] 的值索引您的数组.这样检查它是否已经存在并推送新值会更快.

You can index your array with the value of $row['car']. That way the check if it is already present and pushing new values will be faster.

$array = [];
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        if ($row['timer'] != NULL) {
            if (!array_key_exists($row['car'], $array)) {
                $array[$row['car']] = [
                    'car' => $row['car'],
                    'users' => [
                        ['user' => $row['chatid'], 'timer' => $row['timer']]
                    ],
                    'hunt' => ''
                ];
            } else {
                array_push(
                    $array[$row['car']]['users'],
                    ['user' => $row['chatid'], 'timer' => $row['timer']]
                );
            }
        }
    }
    echo print_r($array, true);
}

这篇关于如何解决:array_push 似乎没有保存附加值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 06:04