Picking up array values

Asked

Viewed 45 times

-1

I have the following array:

array(2) {
  [0]=>
  array(2) {
    ["username"]=>
    string(6) "User01"
    ["quant"]=>
    string(1) "6"
  }
  [1]=>
  array(2) {
    ["username"]=>
    string(5) "Teste"
    ["quant"]=>
    string(1) "3"
  }
 [2]=>
  array(2) {
    ["username"]=>
    string(5) "xxXxx"
    ["quant"]=>
    string(1) "5"
  }
}

How do I take all fields "username" and list, "User01, Test, xxxxx"

I found a function that lists only by array id:

  $resultado[0][username];
  echo $resultado;
  User01

I only printed the set value la[0], but I want you to print all usernames.

Here is the full code, but I only printed the regitro ulitmo.

$sql = 'SELECT username, COUNT(*) AS quant FROM acessos WHERE hora > :hora GROUP BY username';
$statement = $pdo->prepare($sql);
$statement->bindValue(":hora", date('H:i:s', strtotime('-1 minutes')));
$statement->execute();
$resultado = $statement->fetchAll(PDO::FETCH_ASSOC);

//var_dump($resultado);
foreach($resultado as $item){
    $dados = $item["username"];
}
 echo "-> " . $dados;
 -> xxXxx
  • 1

    If you read my answer, you must have realized that the echo has to stay within the loop.

  • It worked right, I just wanted to know which format to be "User01, Test, xxxxx" Thanks for the patience :)

  • 1

    Open another question with this specific problem, please. By the way, if the answer has solved your problem, you can tag it using the ✓ on the left side of the answer.

1 answer

4


Make a loop to go through all the items in the most external array.

foreach($array as $item){
    echo $item["username"] . PHP_EOL;
}

See working on repl.it.

Browser other questions tagged

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