简体   繁体   中英

In PHP and during a foreach loop, how can I replace empty array values with a string?

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 .

如果我了解您想要什么:

$links[] = ($a->href != "") ? $a->href : "Not Found";

Try an if statement that checks if

$a->href

is empty. So the code would look something like this:

if (empty($a->href)) {
    links[] = "Not Found";
} else {
    links[] = $a->href;
}

Looks like there is a bit of confusion here so I am going to write every possible thing that I interpreted as something you want to achieve and I am going off from one of your sentences in the post

I would like to create a condition that would check if that element exists, if not: push into that index value a string.

If you want to see if a specific array key exists, use this

$search_array = array('first' => 1, 'second' => 4);
if (array_key_exists('first', $search_array)) {
    echo "The 'first' element is in the array";
}

You can change the string 'first' to whatever string variable you have in your loop. If you want to see if array key has a "truthy" value, use this:

isset($array['foo'])

Again, you can replace 'foo' with a variable.

If you want to see a specific value exists in an array, use this

$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
    echo "Got Irix";
}

If you are checking string values, it is a good idea to trim the strings before hand. I've had issues in the past where a variable was set to something weird like $var = " " and it acted like it was a true string with length. I like doing something like this

if( strlen( trim($text) ) > 0) { ... }

If any one of those conditions are what you want, just set that index value to a string that you want.

That pretty much sums it up for me. I am sorry I did not understand your question very well and from the looks of it, there are others who feel the same. Good luck to you.

This is what you need :

<?php

foreach($teams as $site) {
    $url     = $site["url"];
    $html    = file_get_html($url);
    $anchors = $html->find($site["selector"];

    if ($anchors !== null) {
        foreach ($html->find($site["selector"]) as $a) {
            $links[] = $a->href; break;
        }
    } else {
        $links[] = 'Not found';
    }
}

?>

But keep in mind that if your selector returns more than one <a /> tag, your final array will not be as you expect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM