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

时间:2018-03-02 18:30:26

标签: php arrays xml array-unique

我的代码正在运行但是重复$categoryurl作为输出的可能性很小,我怎么才能保留唯一身份?

我在webroot中有一个名为“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">' . "\r\n" . '<div class="content_box_header">' . "\r\n\t" . ucfirst($categoryname) . ' &bull; <a href="'. $receptcategoryurl . '">'. $receptcategory . '</a>' . "\r\n" . '</div>' . "\r\n" . '<div class="story_box_text">' . "\r\n" . '<br />' . "\r\n" . '<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>' . "\r\n" . '</div></div>' . "\r\n" . '<div class="clear"></div>' . "\r\n";
}
}
if(empty($html)){
    echo '<p class="error">In verband met werkzaamheden geen inhoud beschikbaar</p>' . "\r\n";
    }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在任何项目中都不应超过一次。< / p>

如果$categoryurl内容重复(在一个或多个其他项目节点内的任何其他$categoryurl中可用),我想保留/添加一个随机项目节点和所有唯一项目。只有100%uniques(已经完成)的$html[]和随机选择的密钥$categoryurl作为$categoryurl内容应该是唯一的网址,如果不是唯一的,则跳过所有重复并保持唯一身份..

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 **/

1 个答案:

答案 0 :(得分:0)

而不是:

bar @Int = False

尝试:

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

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