Mount a result array of a php foreach

Asked

Viewed 186 times

0

Hello!

I am unable to mount an array with results from a foreach.

Got and foreach

foreach($Result as $Aluno):
    extract($Aluno);

    echo '<tr>';
    echo "<td class='nome'>{$Nome}</td>";
    echo "<td class='centro'>{$finalNota}</td>";
    echo '</tr>';
endforeach;       

Displaying:

Aluno | Pontuação
Pedro | 1.5
Ana   | 3
Maria | 5.2
José  | 4

I need to assemble an array with the score having so:

Array
(
    0 => 1.5
    1 => 3
    2 => 5.2
    3 => 4
)

Thank you

  • That’s right. Thank you very much!

1 answer

1


does so on your Cod

foreach($Result as $Aluno):
    extract($Aluno);

    echo '<tr>';
    echo "<td class='nome'>{$Nome}</td>";
    echo "<td class='centro'>{$finalNota}</td>";
    echo '</tr>';
endforeach; 

turns him into this:

$notas=array();
foreach($Result as $Aluno):
    extract($Aluno);

    echo '<tr>';
    echo "<td class='nome'>{$Nome}</td>";
    echo "<td class='centro'>{$finalNota}</td>";
    echo '</tr>';
    $notas[]=$finalNota;
endforeach;       

at the end of foreach you already have the notes array the way you want it.

Browser other questions tagged

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