简体   繁体   English

在PHP中以及在foreach循环中,如何用字符串替换空数组值?

[英]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'. 我正在使用库PHP Simple HTML DOM Parser,并且我的数组运行正常,现在在某些URL中,该元素根本不存在(可以),但是我想创建一个条件来替换空具有字符串的数组值,例如“未找到”。

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循环如下所示:

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' . foreach循环将'selector'找到的href链接的值推入索引。 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: 因此,假设索引2、4和5 href不存在,则预期结果应如下所示:

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 . 我不知道在哪里放置该条件以及适合foreach的正确语法。

如果我了解您想要什么:

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

Try an if statement that checks if 尝试一个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. 您可以将字符串'first'更改为循环中具有的任何字符串变量。 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. 同样,您可以将'foo'替换为变量。

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. 过去我遇到过这样的问题,即变量设置为诸如$var = " "类的变量,它的行为就像是一个带有长度的真实字符串。 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. 但是请记住,如果选择器返回多个<a />标签,则最终数组将与您期望的不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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