3
\$\begingroup\$

I'm scraping a title and a link from a website and placing them in a single array. The $i variable seems a bit wrong to me, or is it perfectly fine?

$linkQuery = $xpath->query("//div[@class='news-archive']//a/@href");
$titleQuery = $xpath->query("//div[@class='news-archive']//a");

$data = [];

foreach ($linkQuery as $link) {
    $data[] = array('link' => $link->value);
}

$i = 0;
foreach ($titleQuery as $title) {
    $data[$i]['title'] = $title->nodeValue;
    $i++;
}

I do like the structure of the Array:

Array (
    [0] => Array
        (
            [link] => http://www.gpupdate.net/nl/f1-nieuws/321783/force-india-vreest-voor-deelname-aan-laatste-test/
            [title] => Force India vreest voor deelname aan laatste test
        )
\$\endgroup\$
5
\$\begingroup\$

I think that one of the collections you iterate already has all the data you need. Try this:

$linkQuery = $xpath->query("//div[@class='news-archive']//a");
foreach ($linkQuery as $link) {
    $data[] = array('link' => $link->getAttribute('href'), 'title' => $link->nodeValue);
}

Btw. array_map() is another way for traversing two (or more) arrays simultaneously:

$group_elements = function($link, $title) {
    return array('link' => $link->value, 'title' => $title->nodeValue);
};
$data = array_map($group_elements, $linkQuery, $titleQuery);
\$\endgroup\$
2
\$\begingroup\$

The "i" is not necessary when you already have the array key.

The key applies to both link and title.

PHP

$linkQuery = array("link1", "link2", "link3", "link4", "link5");
$titleQuery = array("title1", "title2", "title3", "title4", "title5");



foreach($linkQuery as $k => $link){
  $data[$k] = array('link' => $link,'title' => $titleQuery[$k]);
}

.

The Result

array (
  0 => 
  array (
    'link' => 'link1',
    'title' => 'title1',
  ),
  1 => 
  array (
    'link' => 'link2',
    'title' => 'title2',
  ),
  2 => 
  array (
    'link' => 'link3',
    'title' => 'title3',
  ),
  3 => 
  array (
    'link' => 'link4',
    'title' => 'title4',
  ),
  4 => 
  array (
    'link' => 'link5',
    'title' => 'title5',
  ),
)
\$\endgroup\$
1
\$\begingroup\$

using two foreach loops seems redundant

<?php 

//I'm assuming this is the data that you'd recieve in a form of an object and that there are as many links as titles
$linkQuery = array("link1", "link2", "link3", "link4", "link5");
$titleQuery = array("title1", "title2", "title3", "title4", "title5");

$data = array();

if(count($linkQuery) == count($titleQuery)){
    /*
     *if there are as many links as there are titles we start a 
     *for loop that goes through all titles and therefore links too
    */
    for ($i=0; $i < count($titleQuery); $i++) { 
        /*
         *push the link and title inside data array one by one, keep in mind you'd have to replace 
         * $linkQuery and $titleQuery with the values relevant to you
         * they would probaly be $linkQuery[$i]->value and $titleQuery[$i]->nodeValue
        */
        $data[] = array('link' => $linkQuery[$i], 'title' => $titleQuery[$i]);

    }

}

var_dump($data);
/*
returns

array (size=5)
  0 => 
    array (size=2)
      'link' => string 'link1' (length=5)
      'title' => string 'title1' (length=6)
  1 => 
    array (size=2)
      'link' => string 'link2' (length=5)
      'title' => string 'title2' (length=6)
  2 => 
    array (size=2)
      'link' => string 'link3' (length=5)
      'title' => string 'title3' (length=6)
  3 => 
    array (size=2)
      'link' => string 'link4' (length=5)
      'title' => string 'title4' (length=6)
  4 => 
    array (size=2)
      'link' => string 'link5' (length=5)
      'title' => string 'title5' (length=6)


*/

 ?>
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.