How to fix: array_push doesn't seem to save added value

▼魔方 西西 提交于 2020-01-05 06:28:46

问题


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.

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.

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

回答1:


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

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) 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']]);
    }
}

Suggestion for optimization

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


来源:https://stackoverflow.com/questions/57674250/how-to-fix-array-push-doesnt-seem-to-save-added-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!