从数组中删除空/空元素/索引/键

时间:2023-01-07 11:46:01

I am getting separate all url links from HTMl contents with this code

我用这段代码将所有的url链接从HTMl内容中分离出来

$doc = new DOMDocument();
$doc->loadHTML($string);
$anchorTags = $doc->getElementsByTagName('a');
$links = array();
foreach ($anchorTags as $url) {
    $source = parse_url($url->getAttribute('href'));
    $source = preg_replace('/^www\./', '', $source['host']);
    $links[$source][$url->getAttribute('href')] = $url->nodeValue;
}

Output with this above code.

输出上面的代码。

Array
(
    [Facebook] => Array
                (
                    [facebook.com] => https://www.facebook.com/
                )

    [Google] => Array
                (
                    [google.com] => https://www.google.com/
                )

    [] => Array
        (
            [] =>
         )

    [yahoo] => Array
            (
                [yahoo.com] => https://www.yahoo.com/
            )

)

I just want to get remove the null/blank elements/index/key from array for this i am using array_filter();

我只是想从数组中删除null/blank元素/index/key我使用array_filter();

But not getting solution.

但没有得到解决方案。

print_r(array_filter($links));

4 个解决方案

#1


2  

Just add the condition for checking values :

只需添加检查值的条件:

$links = array();
foreach ($anchorTags as $url) {
    $source = parse_url($url->getAttribute('href'));
    $source = preg_replace('/^www\./', '', $source['host']);
    if($source != null && $source != "" && $url->nodeValue != null && $url->nodeValue != ""){
         $links[$source][$url->getAttribute('href')] = $url->nodeValue;
    }
}

#2


1  

Or, a bit more elegant, don't even push results to your array if it's empty:

或者,更优雅一点,如果数组是空的,甚至不要将结果推到数组中:

if ($source != "") $links[$source][$url->getAttribute('href')] = $url->nodeValue;

#3


1  

You can try this,

你可以试试这个,

    // Remove empty elements
foreach($links as $key => $val){
    if($val == '')
    {
        unset($val);
    }
}

#4


0  

you can check on strlen, like this

你可以检查一下strlen,像这样

print_r(array_filter($links, 'strlen' ));

print_r(array_filter(链接,美元' strlen '));

#1


2  

Just add the condition for checking values :

只需添加检查值的条件:

$links = array();
foreach ($anchorTags as $url) {
    $source = parse_url($url->getAttribute('href'));
    $source = preg_replace('/^www\./', '', $source['host']);
    if($source != null && $source != "" && $url->nodeValue != null && $url->nodeValue != ""){
         $links[$source][$url->getAttribute('href')] = $url->nodeValue;
    }
}

#2


1  

Or, a bit more elegant, don't even push results to your array if it's empty:

或者,更优雅一点,如果数组是空的,甚至不要将结果推到数组中:

if ($source != "") $links[$source][$url->getAttribute('href')] = $url->nodeValue;

#3


1  

You can try this,

你可以试试这个,

    // Remove empty elements
foreach($links as $key => $val){
    if($val == '')
    {
        unset($val);
    }
}

#4


0  

you can check on strlen, like this

你可以检查一下strlen,像这样

print_r(array_filter($links, 'strlen' ));

print_r(array_filter(链接,美元' strlen '));