Difficulties in searching for values in an array

1

The following code is an increment code in an array.

However, I have not been able to select any value from the array. If I give print_r to it, it shows all but if I try to access with $guarda_array[1] it returns Array and not the name that I want. Is some of the code wrong?

$guarda_array = array();
$count_pub=mysqli_query($db, "SELECT nome FROM publicidades_sponsors");
$count_tudo=mysqli_num_rows($count_pub);
while($gu=mysqli_fetch_assoc($count_pub)){
    array_push($guarda_array, $gu);
}

The print_r shows the following

Array ( [0] => Array ( [nome] => DIDAXIS1.png ) [1] => Array ( [nome] => DIDAXIS2.png ) [2] => Array ( [nome] => DIDAXIS3.png ) [3] => Array ( [nome] => DIDAXIS4.png ) ) 
    
asked by anonymous 14.06.2018 / 12:57

2 answers

1

If the intention is to result in a array with the "name" field of SELECT , you need to do:

$guarda_array = array();
$count_pub=mysqli_query($db, "SELECT nome FROM publicidades_sponsors");
$count_tudo=mysqli_num_rows($count_pub);
while($gu=mysqli_fetch_assoc($count_pub)){
    array_push($guarda_array, $gu["nome"]);
}
    
14.06.2018 / 13:09
1

The structure of your array looks like this:

Array ( 
    [0] => Array ( 
        [nome] => DIDAXIS1.png 
    ) 
    [1] => Array ( 
        [nome] => DIDAXIS2.png 
    ) 
    [2] => Array ( 
        [nome] => DIDAXIS3.png 
    ) 
    [3] => Array ( 
        [nome] => DIDAXIS4.png 
    ) 
)

Then notice that your attempt is wrong:

$guarda_array[1]

This $guarda_array[1] has an array as its content, so it returned array , to get nome inside $guarda_array[1] :

$guarda_array[1]['nome']
    
14.06.2018 / 13:05