我有这个代码为html类name
中的所有文本创建一个JSON数组。但如果有两个相同的例子:
<a class="name">Hey</a>
<a class="name">Hey</a>
JSON数组看起来像这样
{"names":["Hey"]}
我希望它看起来像这样:
{"names":["Hey","Hey"]}
<?php
$html = file_get_contents('http://xxxxxx.co.uk/xxxxx.html');
function linkExtractor($html)
{
$doc = new DOMDocument();
$last = libxml_use_internal_errors(TRUE);
$doc->loadHTML($html);
libxml_use_internal_errors($last);
$xp = new DOMXPath($doc);
$result = array();
foreach ($xp->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' name ')]") as $node)
$result[trim($node->textContent)] = 1;
return array_keys($result);
}
echo json_encode(array(
"names" => linkExtractor($html)
));
?>
答案 0 :(得分:1)
您将它们用作数组键:
$result[trim($node->textContent)] = 1;
一个键在数组中是唯一的。只需使用
$result[] = trim($node->textContent);
(当然还有return $result;
而不是return array_keys($result);
)
答案 1 :(得分:1)
你在那里努力工作。 : - )
function linkExtractor($html){
$doc = new DOMDocument();
$last = libxml_use_internal_errors(TRUE);
$doc->loadHTML($html);
libxml_use_internal_errors($last);
$xp = new DOMXPath($doc);
$result = array();
foreach ($xp->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' name ')]") as $node)
$result[] = trim($node->textContent); // Just push the result here, don't assign it to a key (as that's why you're overwriting)
// Now return the array, rather than extracting keys from it
return $result;
}