I
I
iurakuleshov2020-05-23 14:14:03
PHP
iurakuleshov, 2020-05-23 14:14:03

Question with Foreach, how to get a variable?

Hello, here is the code

$id = $this->database->all("customs");     - тут получаем все значения из таблицы
$key_all = array_column($id, 'user_id');     - из полученных значений вытаскиваем ключи
$keys = array_unique($key_all);     -    убираем ключи с дублирующими значениями 
 foreach ($keys as $key) {     
     $users = $this->database->whereAll("users", 'id', $key);
}

There may be an unknown number of values ​​\u200b\u200breceived in the $keys variable, through forech I want to make it pull out a line for each value, but as a result, when I wardump the $users variable, I get the last value from the received table, maybe somehow I’m not right all done, please advise.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Ryazantsev, 2020-05-23
@iurakuleshov

Well, you're smart :) You overwrite the value of the $users variable with each iteration of the loop. Declare this variable as an array, and instead of overwriting, append the values ​​to the end. Like this:

$id = $this->database->all("customs");     //тут получаем все значения из таблицы
$key_all = array_column($id, 'user_id');     //из полученных значений вытаскиваем ключи
$keys = array_unique($key_all);     //убираем ключи с дублирующими значениями
$users = array(); //Инициализируем массив с пользователями
 foreach ($keys as $key) {     
     $users[] = $this->database->whereAll("users", 'id', $key); //Две скобочки решают 
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question