How can I add an array within an array?

Asked

Viewed 211 times

2

I need a multidimensional array that will be composed of database information, but I’m not able to add this information. I can’t explain it very well, so I’ll show you.

So I want to:

Array
(
    [0] => Array
        (
            [valor_apostado] => 100
            [valor_final] => 817
            [id_aposta] => 1022
            [0] => Array
                (
                    [timeApostado] => Motherwell
                        [partidaApostada] => Motherwell x Hearts
                    )
                [1] => Array
                    (
                        [timeApostado] => Hearts
                        [partidaApostada] => Pandora x Hearts
                    )

            )
)

But he comes like this:

Array
(
    [0] => Array
        (
            [valor_apostado] => 100
            [valor_final] => 817
            [id_aposta] => 1022
            [0] => Array
                (
                    [timeApostado] => Motherwell
                    [partidaApostada] => Motherwell x Hearts
                )

        )

The other part of the array within the array is not displayed.

My code:

$idAnt = 0;
$array = "";
$i = 0;
$n = 0;
while($dado = $pegar->fetch(PDO::FETCH_ASSOC)) {
    $valor_apostado = $dado["valor_apostado"];
    $valor_final = $dado["valor_final"];
    $tipo_aposta = $dado["tipo_aposta"];
    $time = $dado["nome_time"];
    $idAposta = $dado["id_aposta"];

    if($idAposta == $idAnt) {
      $x[$n] = array(
        "timeApostado" => $tipo_aposta,
        "partidaApostada" => $time
      );
      $n++;
    } else {
      $n = 0;
      $x = array(
        "valor_apostado" => $valor_apostado,
        "valor_final" =>$valor_final,
        "id_aposta" => $idAposta,
        $n => array(
          "timeApostado" => $tipo_aposta,
          "partidaApostada" => $time
        )
      );
      $array[$i]= $x;
      $i++;
      $n++;
    }
    $idAnt = $idAposta;

}
  • When you do the database search, is all the information brought in? If yes, try to do this: $x[$n][]

  • Yes, all at once. I’ll try!

2 answers

3


Its code seems to me correct, being only with error when assigning the values to the array. You are resetting the counter whenever there is an equal answer. Right after the command else remove the condition $n = 0;.

Your code would look like this:

  if($idAposta == $idAnt) {
      $x[$n] = array(
        "timeApostado" => $tipo_aposta,
        "partidaApostada" => $time
      );
      $n++;
    } else {
      $x = array(
  • Actually, I need the $n to be zero again because there can be both ways, you know? There can be only one child array within the parent array or several child arrays within the parent array. So if there are no more child arrays within the parent after it has already added all existing children, the code needs to add one more parent with their own children within the grandfather array.

  • 1

    So there is if too, because if the new id_bet is different from the previous one, then it will add a new parent array

  • @Krint, it’s possible to do what you want, but it’s going to require a slightly more complex algorithm. An easier solution is to keep the changes I suggested and proceed to order your search in the database. This way there’s no way to see another father’s child again.

2

You can also change your code to use the $idAposta as an index.

$array = array();
while($dado = $pegar->fetch(PDO::FETCH_ASSOC)) {
    $valor_apostado = $dado["valor_apostado"];
    $valor_final = $dado["valor_final"];
    $tipo_aposta = $dado["tipo_aposta"];
    $time = $dado["nome_time"];
    $idAposta = $dado["id_aposta"];

    if(array_key_exists($idAposta, $array)) {
        $array[$idAposta][] = array(
            "timeApostado"      => $tipo_aposta,
            "partidaApostada"   => $time
        );
    } else {
        $array[$idAposta] = array(
            "valor_apostado"    => $valor_apostado,
            "valor_final"       =>$valor_final,
            "id_aposta"         => $idAposta,
            0 => array(
                "timeApostado"  => $tipo_aposta,
                "partidaApostada" => $time
            )
        );
    }
}
  • Didn’t work out the way I needed to

Browser other questions tagged

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