0

I have 2 xPath requests :

$medias = $xpath->query("//strong//a[contains(@class, 'no')]");
$links =  $xpath->query("//strong//a[contains(@class, 'no')]/@href");

My goal is to have only one array that contains something like this :

0 => array:1 [▼
    "title" => "A besúgó"
    "link" => "xyz"
]

I tried this

    $i=0;
    foreach($medias as $media)
    {
        $tab[]['titre'] = $media->textContent;
        $i++;
    }
    $i=0;
    
    foreach($medias as $media)
    {
        $tab[]['lien'] = $media->textContent;
        $i++;
    }
    
    
    
    dd($tab);

But it's ugly and it does not work. Can you help ?

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • https://www.php.net/manual/en/function.array-merge.php – dave Jul 30 '22 at 18:11
  • @dave i don't think they are trying to do a straight merge however. i think they are trying to create more structured data out of the two arrays, with each element containing an associative array with two named elements. the question is poorly worded. – dqhendricks Jul 30 '22 at 18:15
  • What is the use of $i here? – nice_dev Jul 30 '22 at 18:15

2 Answers2

1

It's hard to understand what you are trying to achieve, but from what I can tell it is this.

$tab = [];
for ($i = 0; $i < count( $medias ); $i++ ) {
    $tab[] = [ 'titre' => $medias[$i], 'lien' => $links[$i] ];
}
dqhendricks
  • 19,030
  • 11
  • 50
  • 83
1

you may just use array_merge() function to merging more than 1 arrays

array_merge(array1, array2, array3, ...)