为什么这个数组不想删除它的副本?

时间:2011-11-16 02:02:21

标签: php arrays

我有一个查看基域URL(http://www.site.com)的例程,查找所有链接,然后查找每个页面的所有图像及其属性。这是在两个for循环中完成的:

  • 首先是链接,每个链接的每个循环内部
  • 每页上找到一张图片。

我一直在使用我的乐队的网站作为试验台,顶部的每个页面都有一个特色文章的“聚光灯”部分,它被设置为图像滑块。所以,我只想要一个网站的独特图像网址,但我正在尝试的每一件事仍然让重复。在构建阵列时我曾尝试过进行欺骗检查,但那没有结果。但后来我找到了这个链接:How to remove duplicate values from a multi-dimensional array in PHP和评论,但这也不起作用。

让我们从我的乐队网站上搜集的一系列数据示例开始:

Array
(
[http://darwenstheory.com/] => Array
    (
        [0] => Array
            (
                [3] => Array
                    (
                        [url] => http://darwenstheory.com/images/dtheory-spotlight-vidclips.jpg
                        [alt] => Ventura Theater Video Clips Posted!
                        [w] => 644
                        [h] => 202
                        [ratio] => 3.2
                    )

            )

        [1] => Array
            (
                [3] => Array
                    (
                        [url] => http://darwenstheory.com/images/dtheory-spotlight-vtpix.jpg
                        [alt] => Video Clips Posted!
                        [w] => 644
                        [h] => 202
                        [ratio] => 3.2
                    )

            )

        [2] => Array
            (
                [3] => Array
                    (
                        [url] => http://darwenstheory.com/images/dtheory-spotlight-merch.jpg
                        [alt] => Photos from Ventura Theater!
                        [w] => 644
                        [h] => 202
                        [ratio] => 3.2
                    )

            )

        [3] => Array
            (
                [4] => Array
                    (
                        [url] => http://darwenstheory.com/wp-content/uploads/2011/10/peepdestroyflyer.jpg
                        [alt] => 
                        [w] => 533
                        [h] => 800
                        [ratio] => 0.7
                    )

            )
[http://darwenstheory.com/2011/01/11/ventura-theater-video-clips-posted/] => Array
    (
        [0] => Array
            (
                [3] => Array
                    (
                        [url] => http://darwenstheory.com/images/dtheory-spotlight-vidclips.jpg
                        [alt] => Ventura Theater Video Clips Posted!
                        [w] => 644
                        [h] => 202
                        [ratio] => 3.2
                    )

            )

        [1] => Array
            (
                [3] => Array
                    (
                        [url] => http://darwenstheory.com/images/dtheory-spotlight-vtpix.jpg
                        [alt] => Video Clips Posted!
                        [w] => 644
                        [h] => 202
                        [ratio] => 3.2
                    )

            )

        [2] => Array
            (
                [3] => Array
                    (
                        [url] => http://darwenstheory.com/images/dtheory-spotlight-merch.jpg
                        [alt] => Photos from Ventura Theater!
                        [w] => 644
                        [h] => 202
                        [ratio] => 3.2
                    )

            )

在上面的数组中,我不应该有第二个索引的前三个图像URL(这是域上子页面的URL)。 我用来构建数组的简化版本:

foreach($links as $link)
{
    $images = get_page_images($link); //array;
    foreach($images as $image)
    {
        //i have some things here to setup a "score" for each image
        $data['scrape'][$link][][$score] = array('url' => $image['url'], 'alt' => $image['alt'], 'w' => $image['w'], 'h' => $image['h'], $ratio);
    }
}

我有一种感觉,我过于复杂,但我不知道如何或为什么。我在这里学习,无论是愚蠢还是别的什么。

我希望上面建立的数组在最深层数组中没有'url'键的重复值。

非常感谢你,提前批评,帮助,以及所有事情。

2 个答案:

答案 0 :(得分:0)

在构建阵列时我仍然会进行欺骗检查:

$urls = array();

foreach($links as $link)
{
    $images = get_page_images($link); //array;
    foreach($images as $image)
    {
        if (!$urls[$image['url']])       // <- dupe check added
        {
            $urls[$image['url']] = true; // <- dupe check added

            //i have some things here to setup a "score" for each image
            $data['scrape'][$link][][$score] = array('url' => $image['url'], 'alt' => $image['alt'], 'w' => $image['w'], 'h' => $image['h'], $ratio);
        }
    }
}

答案 1 :(得分:0)

这要看很多,但是我可能会建议启动一个基本数组来比较每次迭代,如果基本数组中不存在该键,则只添加到数组中...

$image_arr = array();
foreach($links as $link)
{

  $images = get_page_images($link); //array;
  foreach($images as $image)
  {
      if(!in_array($image['url'], $image_arr))
      {  
            //i have some things here to setup a "score" for each image
            $data['scrape'][$link][][$score] = array('url' => $image['url'], 'alt' => $image['alt'], 'w' => $image['w'], 'h' => $image['h'], $ratio);
            $images_arr[$image['url'] = $image['url';
      }
  }
}