Help with PHP multidimensional and foreach array

Asked

Viewed 34 times

0

all right with you guys?

I’m studying PHP programming and mainly reading Apis, I came across the.com ticket api for reading movies and sessions, I’m managing to capture the sessions of a certain movie, only I’m having difficulty to popular the information in a single array.

I can capture the sessions only of a specific cinema, by means of an ID: $value-id == '1448'

I handle all API information, only with the data I need and save in the $Events array[];

My code is like this:

$idFilme = 23800;
$url = "https://api-content.ingresso.com/v0/sessions/city/283/event/".$idFilme."/partnership/teste";
$sessoes = json_decode(file_get_contents($url, true));

foreach ($sessoes as $key => $value) {
    foreach ($value->theaters as $indice => $valor) {
        if ($valor->id == '1448') { // ID DO CINEMA NO INGRESSO.COM (theater)

$salas = $valor->rooms;

foreach ($salas as $sala) {

$sessoes = $sala->sessions;

foreach ($sessoes as $sessao) {

$nomeSala = $sala->name;
$diaSessao = $sessao->realDate->dayOfWeek;
$horarioSessao = $sessao->realDate->hour;
$tipoSessao = $sessao->types[0]->alias.$tipoSessao2 = $sessao->types[1]->alias.$tipoSessao3 = $sessao->types[2]->alias. $tipoSessao4 = $sessao->types[3]->alias.$tipoSessao5 = $sessao->types[4]->alias;
$idSessao = $sessao->id;
$carrinhoSessao = $sessao->siteURL;
$dataSessao = $sessao->realDate->dayAndMonth."/".$sessao->realDate->year;

if (strpos($tipoSessao, "DUB") !== false) {$dub = TRUE;}
if (strpos($tipoSessao, "2D") !== false) {$doisd = TRUE;}
if (strpos($tipoSessao, "LEG") !== false) {$leg = TRUE;}
if (strpos($tipoSessao, "3D") !== false) {$tresd = TRUE;}
if (strpos($tipoSessao, "NAC") !== false) {$nac = TRUE;}
if (strpos($tipoSessao, "VIP") !== false) {$vip = TRUE;}
if (strpos($tipoSessao, "IMAX") !== false) {$imax = TRUE;}
if (strpos($tipoSessao, "Kino") !== false) {$kino = TRUE;}
if (strpos($tipoSessao, "DRIVEIN") !== false) {$drivein = TRUE;};

$events = array();
$events[] = array(
array(
'sala_sessao'  => $nomeSala,
'id_sessao'   => $idSessao,
'data_sessao'   => $dataSessao,
'dia_sessao'  => $diaSessao,
'leg'  => $leg,
'dub'  => $dub,
'nac'  => $nac,
'2d'  => $doisd,
'3d'  => $tresd,
'vip'  => $vip,
'imax'  => $imax,
'kino'  => $kino,
'drivein'  => $drivein,
'horario_sessao'  => $horarioSessao
)
);


print_r($events);

}
}
        }
     }
  }

My result is that way:

  • It creates multiple arrays with each session;
  • It has no indices, only [0].
Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [sala_sessao] => Sala 1
                    [id_sessao] => 65169829
                    [data_sessao] => 22/12/2020
                    [dia_sessao] => terça-feira
                    [leg] => 
                    [dub] => 1
                    [nac] => 
                    [2d] => 
                    [3d] => 
                    [vip] => 
                    [imax] => 
                    [kino] => 1
                    [drivein] => 
                    [horario_sessao] => 17:00
                )

        )

)
Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [sala_sessao] => Sala 1
                    [id_sessao] => 65169828
                    [data_sessao] => 22/12/2020
                    [dia_sessao] => terça-feira
                    [leg] => 
                    [dub] => 1
                    [nac] => 
                    [2d] => 
                    [3d] => 
                    [vip] => 
                    [imax] => 
                    [kino] => 1
                    [drivein] => 
                    [horario_sessao] => 20:00
                )

        )

)

The result I need:

  • Create a single array;
  • Popular all sessions in this single array;
  • Create indices for each session in the array.
Array
(
    [0] => Array
        (
    
                    [sala_sessao] => Sala 1
                    [id_sessao] => 65169829
                    [data_sessao] => 22/12/2020
                    [dia_sessao] => terça-feira
                    [leg] => 
                    [dub] => 1
                    [nac] => 
                    [2d] => 
                    [3d] => 
                    [vip] => 
                    [imax] => 
                    [kino] => 1
                    [drivein] => 
                    [horario_sessao] => 17:00
                )

[1] => Array
        (
                    [sala_sessao] => Sala 1
                    [id_sessao] => 65169828
                    [data_sessao] => 22/12/2020
                    [dia_sessao] => terça-feira
                    [leg] => 
                    [dub] => 1
                    [nac] => 
                    [2d] => 
                    [3d] => 
                    [vip] => 
                    [imax] => 
                    [kino] => 1
                    [drivein] => 
                    [horario_sessao] => 20:00
                )





)

Would a charitable soul help me? I’ve already broken my head and I don’t know where I’m going wrong to be able to organize it. Could give me a light?

A hug and a 2021 hope for all of us.

No answers

Browser other questions tagged

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