Gonçalo, I took as a basis the gonçalo’s answer but simplified the loop:
$postos = array();
while($array1 = mysqli_fetch_array($procura1)) $postos[] = $array1['posto'];
This works because when you do variavel[] = 'valor';
the value is added at the end of the array.
Depending on the desired structure, you have an even simpler alternative:
$tudo = mysqli_fetch_all($procura1);
This takes all the results at once, without having to do a loop manually. Only in this case, you will receive a array of arrays and not of values.
If you have PHP 5.5 or higher, you can use fetch_all
thus:
$tudo = mysqli_fetch_all($procura1, MYSQLI_ASSOC); // Pegamos todas as linhas
$postos = array_column($tudo, 'posto'); // extraimos só a coluna 'posto'
The MYSQLI_ASSOC
is for columns to come indexed with names instead of numbers. If we used MYSQLI_NUM
it would be necessary array_column($tudo, numero_da_coluna)
, what complicates a little maintenance if something is changed in the SELECT
.
More details in the manual:
http://php.net/manual/en/mysqli-result.fetch-all.php
http://php.net/manual/en/function.array-column.php
I realize that it has to be with while. however I intend to transform the value in array.
– Gonçalo