如何从包含重复URL的数组中删除数组键?



我的代码正在工作,但有很小的可能性将重复$categoryurl作为输出,我怎样才能只保留唯一?

我在网络根目录中有一个名为"xml"的文件夹,我使用glob()/xml/目录中搜索 xml 文件。

我使用循环来查找所有 XML 文件并查找所有项目节点,项目节点可以复制,因为某些节点在多个 xml 文件中可用,因此我使用$html = array_unique($html);来保留所有 100% 唯一性并从我的数组中删除所有重复项。

一些代码:

<?php
// Removed the code above this line as it's not needed in this question 
// $URL_array is defined above, it's an array() filled with XML URL's
foreach($URL_array as $XML_url){
$xml = simplexml_load_file($XML_url);
if ($xml===null || !is_object($xml))
die('Kon het XML bestand niet laden, Raporteer a.u.b. deze fout.');
if (!is_object($xml->item))
die('Kon de items niet laden, rapporteer a.u.b. deze fout.');
$Number_Of_Nodes = $xml->item->count(); /** Count number of items **/
for($i = 0; $i < $Number_Of_Nodes; $i++){ /** Number of category here... **/
$categoryname = $xml->item[$i]->recepttitle;
$categoryurl = $xml->item[$i]->recepturl;
$receptintroduction = $xml->item[$i]->receptintroduction;
$receptimageurl = $xml->item[$i]->receptimageurl;
$receptcategoryurl = $xml->item[$i]->receptcategoryurl;
$receptcategory = $xml->item[$i]->receptcategory;
$html[] = '<div class="content_box">' . "rn" . '<div class="content_box_header">' . "rnt" . ucfirst($categoryname) . ' &bull; <a href="'. $receptcategoryurl . '">'. $receptcategory . '</a>' . "rn" . '</div>' . "rn" . '<div class="story_box_text">' . "rn" . '<br />' . "rn" . '<p><a title="' . $categoryname . '" href="' . $categoryurl . '"><img src="' . $receptimageurl . '" alt="' . $categoryname . '" title="' . $categoryname . '" /></a><br />' . $receptintroduction . '<br /><span class="align-right"><a title="'. $categoryname . '" href="' . $categoryurl . '" class="purplesmallbutton">Lees verder</a></span><br /></p>' . "rn" . '</div></div>' . "rn" . '<div class="clear"></div>' . "rn";
}
}
if(empty($html)){
echo '<p class="error">In verband met werkzaamheden geen inhoud beschikbaar</p>' . "rn";
}else{
$html = array_unique($html); /** Alle duplicaten verwijderen **/
shuffle($html);

现在我有一个装满字符串的大型随机数组

如果$categoryurl是重复的,我想保留找到的第一个密钥和所有唯一密钥,我应该如何实现这一点?

我的代码的最后一部分:

echo implode("n", array_slice($html, 0, 6)); /** output shoud always be 6 array keys only without duplicated $categoryurl, if there are duplicates the duplicates should be removed before this action **/
}
?>

编辑

XML 文件的内容:1.xml

<?xml version="1.0" encoding="UTF-8"?>
<channel xmlns="http://www.w3.org/2005/Atom">
<id>...</id>
<title><![CDATA[vegetarische hoofdgerechten RSS]]></title>
<author>
<name>Voeding vegetarische hoofdgerechten</name>
<email>webmaster@example.com</email>
</author>
<updated></updated>
<link rel="alternate" href="https://voeding.esthervrees.nl/vegetarische-hoofdgerechten" />
<subtitle><![CDATA[Some subtitle here.]]></subtitle>
<rights>Copyrights reserved. Feel free to use the embed function.</rights>
<item>
<recepttitle><![CDATA[Pittige rijst met bonen voor 6 tot 8 personen]]></recepttitle>
<shortrecepttitle><![CDATA[Pittige rijst met bonen]]></shortrecepttitle>
<receptintroduction>Some cool description here Some cool description here Some cool description here Some cool description here Some cool description here Some cool description here Some cool description here Some cool description here Some cool description here Some cool description here Some cool description here Some cool description here Some cool description here </receptintroduction>
<recepturl>https://google.com</recepturl>
<receptimageurl>https://voeding.esthervrees.nl/plaatjes/werk-aan-de-winkel-geen-afbeelding-beschikbaar-610x550px.gif</receptimageurl>
<receptcategoryurl>https://www.yahoo.com</receptcategoryurl>
<receptcategoryimage>https://voeding.esthervrees.nl/plaatjes/werk-aan-de-winkel-geen-afbeelding-beschikbaar-610x550px.gif</receptcategoryimage>
<receptcategory>Vegetarische hoofdgerechten</receptcategory>
</item>
</channel>

XML 文件的内容:2.xml

<?xml version="1.0" encoding="UTF-8"?>
<channel xmlns="http://www.w3.org/2005/Atom">
<id>...</id>
<title><![CDATA[vegetarische hoofdgerechten RSS]]></title>
<author>
<name>Voeding vegetarische hoofdgerechten</name>
<email>webmaster@example.com</email>
</author>
<updated></updated>
<link rel="alternate" href="https://voeding.esthervrees.nl/vegetarische-hoofdgerechten" />
<subtitle><![CDATA[Some subtitle here.]]></subtitle>
<rights>Copyrights reserved. Feel free to use the embed function.</rights>
<item>
<recepttitle><![CDATA[Pittige rijst met bonen voor 6 tot 8 personen]]></recepttitle>
<shortrecepttitle><![CDATA[Pittige rijst met bonen]]></shortrecepttitle>
<receptintroduction>Some cool description here Some cool description here Some cool description here Some cool description here Some cool description here Some cool description here Some cool description here Some cool description here Some cool description here Some cool description here Some cool description here Some cool description here Some cool description here </receptintroduction>
<recepturl>https://google.com</recepturl>
<receptimageurl>https://voeding.esthervrees.nl/plaatjes/werk-aan-de-winkel-geen-afbeelding-beschikbaar-610x550px.gif</receptimageurl>
<receptcategoryurl>https://www.yahoo.com</receptcategoryurl>
<receptcategoryimage>https://voeding.esthervrees.nl/plaatjes/werk-aan-de-winkel-geen-afbeelding-beschikbaar-610x550px.gif</receptcategoryimage>
<receptcategory>Vegetarische hoofdgerechten</receptcategory>
</item>
<item>
<recepttitle><![CDATA[Pittige rijst met bonen voor 6 tot 8 personen]]></recepttitle>
<shortrecepttitle><![CDATA[Pittige rijst met bonen]]></shortrecepttitle>
<receptintroduction>Some cool description here Some cool description here Some cool description here Some cool description here Some cool description here Some cool description here Some cool description here Some cool description here Some cool description here Some cool description here Some cool description here Some cool description here Some cool description here </receptintroduction>
<recepturl>https://yahoo.com</recepturl>
<receptimageurl>https://voeding.esthervrees.nl/plaatjes/werk-aan-de-winkel-geen-afbeelding-beschikbaar-610x550px.gif</receptimageurl>
<receptcategoryurl>https://www.yahoo.com</receptcategoryurl>
<receptcategoryimage>https://voeding.esthervrees.nl/plaatjes/werk-aan-de-winkel-geen-afbeelding-beschikbaar-610x550px.gif</receptcategoryimage>
<receptcategory>Vegetarische hoofdgerechten</receptcategory>
</item>
</channel>

XML 文件 1.xml 中的项节点和第一项节点确实具有相同的$categoryurl内容,但$categoryurl在任何项中都不应多次可用。

如果$categoryurl内容重复(在一个或多个其他项目节点内的任何其他$categoryurl中可用),我只想保留/添加一个随机项目节点和所有唯一项目。 只有 100% 唯一性(已经完成)和随机选择的键$categoryurl具有重复$categoryurl作为内容的$html[]应该是唯一的 URL,如果不是唯一的,请跳过所有重复项并仅保留唯一性。

PHP 示例数组:

$URL_array = array($_SERVER['DOCUMENT_ROOT'] . '/xml/1.xml', $_SERVER['DOCUMENT_ROOT'] . '/xml/xml2.xml'); /** I added a lot more xml files to this array **/

而不是:

$html[] = '<div class="content_box">...';

尝试:

if (array_key_exists($receptcategoryurl, $html)) {
// this key already exists, we proceed to
// the next item in the loop
continue;
}
$html[$receptcategoryurl] = '<div class="content_box">...';

这样,您可以确保只分配第一个匹配项并跳过下一个匹配项。

最新更新