How can I make a select and turn it into an array

Asked

Viewed 491 times

1

Well, from the title of the question I couldn’t explain very well what I wanted but here it goes in practice:

I have a select to a table called "posts".

This select wants me to indicate all posts, where the supervisor is the "Test".

After that I make a mysqli_fetch_array

And then I show you $array["post"];

However, it only shows me 1 rank, and in fact the supervisor "Test" has more than one rank.

How can I transform select posts into 1 array?

Thank you.

  • I realize that it has to be with while. however I intend to transform the value in array.

3 answers

4


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

1

So that you can make use of looping php.

Example with the While

while($vetor = mysqli_fetch_array($procura1)){
   echo $vetor["posto"] . '<br/>';
}

Example with the For

$dados = mysqli_fetch_array($procura1)
for($i = 0; $i < count($dados); $i++){
   echo $dados[$i]["posto"] . '<br/>';
}

Example with the Foreach

$dados = mysqli_fetch_array($procura1)
foreach($dados as $dado){
   echo $dado["posto"] . '<br/>';
}

-1

Gonçalo, you can do it this way:

$contador = 0;
while($array1 = mysqli_fetch_array($procura1)){

$postos[$contador] = $array1["posto"];

$contador++;
}
  • Obrigado Gonçalo!

  • 1

    Gonçalo, it’s nice you want to help Gonçalo, but your code can be simplified like this, in just one line: while($array1 = mysqli_fetch_array($procura1)) $postos[] = $array1['posto']; no counter. Empty PHP square brackets means add to the end of the array. PS: The negatives are not mine, although it is not a very elegant solution, it is not wrong. And it is important to know that there is also the mysqli_fetch_all(); if you want to take it all at once without needing the while. The following manual: http://php.net/manual/en/mysqli-result.fetch-all.php

  • Thank you Gonçalo and Bacco, I will try to use your Bacco solution. It looks much simpler.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.