How to join different foreach results in an array?

Asked

Viewed 289 times

1

I’m picking up information from movie posters from a movie site, using Simple Html Dom.

<?php 
include("simple_html_dom.php");

$html = file_get_html("http://arcoplex.com.br/?lang=059");

To get the titles of the movies I make:

foreach($html->find('.Cartaz .filme-single-name text') as $titulo) {
    echo $titulo . '<br>';
}

To get the src of the movie cover images I make:

foreach($html->find('.Cartaz .filme-img') as $capa) {
    echo $capa->src . '<br>';
}

To get the link of the movies I make:

foreach($html->find('.Cartaz .mais_info a') as $link) {
    echo $link->href . '<br>';
}

The result is like this:

Knowing all this, how do I join the information of each movie together in an array/json?

Example:

{
  filmes: {
    1: {
      titulo: 'A FREIRA',
      capa: 'http://arcoiriscinemas.com.br/2014/wp-content/uploads/2018/08/mini2-1-175x285.jpg',
      link: 'http://arcoplex.com.br/filme/a-freira-2/?lang=059'
    },
    2: ...............,
    3: ...............,
    etc,
  }
}

2 answers

1

You can use the array_map passing by null like callback

$unido = array_map(
    null,
    $html->find('.Cartaz .filme-single-name text'),
    $html->find('.Cartaz .filme-img'),
    $html->find('.Cartaz .mais_info a')
);

0


<?php
    $titulo =  $html->find('.Cartaz .filme-single-name text');
    $capa =  $html->find('.Cartaz .filme-img');
    $link = $html->find('.Cartaz .mais_info a');
    $novaArray = array();
    foreach($titulo as $key => $value) {
        $novaArray[$key] = array('titulo'=>$titulo[$key],'capa'=>$capa[$key],'link'=>$link[$key]);
    }
    print_r($novaArray)

?>

Browser other questions tagged

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