I'm using the library PHP Simple HTML DOM Parser and my array is working A-OK, now in some of the URLs parsed the element simply does not exist (which is OK) yet I would like to create a condition that would replace empty array values with a string such as 'not found'.
How would I be able to accomplish this?
Here is the code:
$memb1 = 'http://www.xyz1.org';
$memb2 = 'http://www.abc3.org';
$memb(n) = '...etc...etc'
$teams = array(
array("url" => $memb1, "selector" => ".product-list >
table:nth-child(1) >
tbody:nth-child(1) >
tr:nth-child(2) >
td:nth-child(2) > a"),
array("url" => $memb2, "selector" => ".product-list >
table:nth-child(1) >
tbody:nth-child(1) >
tr:nth-child(2) >
td:nth-child(2) > a"),
array("url" => $memb(n), "selector" => ".product-list >
table:nth-child(1) >
tbody:nth-child(1) >
tr:nth-child(2) >
td:nth-child(2) > a"),...etc...etc
And my Foreach loop looks like this:
foreach($teams as $site) {
$url = $site["url"];
$html = file_get_html($url);
foreach ($html->find($site["selector"]) as $a) {
$links[] = $a->href; break;
}
}
?>
<pre>
<?php print_r($links);?>
</pre>
So I'll reiterate for clarification; The foreach loop pushes into index the value of the found href links from 'selector'. When an element is not found it just skips to the next index, I would like to create a condition that would check if that element exists, if not: push into that index value a string.
so let's say that index 2, 4, and 5 href's do not exist, the expected result should look like this:
Array
(
[0] => http://www.abcd.com/etc/etc
[1] => http://www.gfege.com/etc/etc
[2] => Not Found
[3] => http://www.asdad.com/etc/etc
[4] => Not Found
[5] => Not Found
)
I have no idea where to place that condition and the right syntax that would fit in foreach.
array[key]isempty()or not andif( empty( array[key] ) )setarray[key] = "Not Found";right ? :)$links[] = $a->href;doesn't get run at all on <a> tags without href? That's a bit strange, the foreach() should include all <a> tags that matches the selector regardless of attributes to it..$html->find($site["selector"])finds only oneatag ?