重复的数组值不适用于简单HTML DOM

时间:2013-07-29 17:02:20

标签: php arrays variables simple-html-dom

我正在使用Simple HTML DOM:

foreach($html->find('img', 18) as $d) {
 echo $d->outertext;
}

现在我想实现一个变量数组,在本例中是图像,所以我做了:

$img=array(

  "img"=>"18",
  "img"=>"21"

);


foreach($img as $x=>$x_value) 
{

  $d = $html->find($x, $x_value);
   echo $d->outertext;

}

问题是简单的HTML DOM只返回数组中的最后一个图像,即21号。我需要做什么才能返回数组中的所有内容?

1 个答案:

答案 0 :(得分:1)

这是因为$img数组中的两个项都具有相同的键。 foreach无法将它们识别为两个单独的项目,因为这两个键都是img

示例代码:

$test = array(
    "key" => 1,
    "key" => 2
);

echo "Length of array: " . count($test) . "\n\n";

echo "Items in array:\n";
foreach($test as $key => $value) {
    echo "$key => $value\n";
}

输出:

Length of array: 1

Items in array:
key => 2
相关问题