My code is working but there is a small possibility to have duplicated $categoryurl
as output, how can I keep the uniques only?
I have a folder called "xml" in the webroot, I use glob()
to search the /xml/
directory for the xml files.
I use a loop to find all XML files and find all item nodes, the item nodes can be duplicated as some of the nodes are available in multiple xml files so I use $html = array_unique($html);
to keep all 100% uniques and remove all duplicates from my array.
Some code:
<?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) . ' • <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);
Now I have a large shuffled array full of strings
if $categoryurl
is duplicated I'd like to keep the first key found and all uniques, How should I achive this?
Last part of my code:
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 **/
}
?>
Edit
Contents for XML file: 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>
Contents for XML file: 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>
the item node in the XML file 1.xml and the first item node do have the same content for $categoryurl
but the $categoryurl
should not be available more then once in any of the items.
If the $categoryurl
content is duplicated (available in any other $categoryurl
inside one or more of the other item nodes) I would like to keep/add only one random item node and all unique items. $html[]
with only 100% uniques (already done with) and a randomly selected key that has a duplicated $categoryurl
as $categoryurl
content should be an unique URL, if not unique, skip all of the duplicates and keep uniques only..
php example array:
$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 **/