The Stack Overflow podcast is back! Listen to an interview with our new CEO.
5

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) ...


3

Whenever chaining awk together with grep, sed, head, tail or other text processor tools, consider the possibility of doing it all with just awk, as it's very powerful. Reducing the number of processes in the pipeline makes sense, and in some examples can drastically improve performance. Here's one way to write the two lines of pipelines as a single line (...


2

I have been told that querying the DOM inside of a loop should be avoided That advice must refer to a specific situation which does not apply here (maybe animation or something?). There's nothing wrong with looping over DOM queries - as you say, that's what makes sense here, as you have a list of things in the DOM. However, you can clean things up ...


2

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]); } ....


1

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($...


Only top voted, non community-wiki answers of a minimum length are eligible