2
I’m parsing from a website, I want to get some data from it, the data is structured as follows:
<div class="interesses">
<span class="tipo" >Tipo 1</span>
<span class="tipo" >Tipo 1</span>
<span class="tipo" >Tipo 2</span>
<span class="tipo" >Tipo 2</span>
<span class="tipo" >Tipo 3</span>
<span class="tipo" >Tipo 3</span>
</div>
I want to get the information inside the span
tipo
, so I used the DOM:
$html = file_get_contents("http://exemplo.com");
$DOM = new DOMDocument();
$DOM->loadHTML('<meta charset="utf-8">'.$html);
$xpath = new DomXpath($DOM);
$tipo = $xpath->query('//*[contains(concat(" ", normalize-space(@class), " "), "tipo")]');
$arrValues = array_map(null,iterator_to_array($tipo))
foreach($arrValues as $value){
echo $value[0]->nodeValue."<br />";
}
Works!
But the problem is that on the source page, as you’ve seen, there are two "type 1" and two "type 2" and so on, the site always generates duplicate information, but I’m only interested in showing one of each, that is, just a "Type 1" and another "Type 2" and so on. But everything is coming and I have no idea what to do to prevent duplicity.
Updating:
The array_unique
that @Miguel Angelo taught, it worked!
But now imagine the following scenario:
There is 1 bakery online that sells various types of sweet bread: coconut and coconut free.
The buyer then chooses two buns one with coconut and the other without coconut, the HTML structure would look something like this:
<div class="interesses">
<span class="tipo" >Pão Doce</span>
<span class="tipo" >Com coco</span>
<span class="tipo" >Pão Doce</span>
<span class="tipo" >Com coco</span>
<span class="tipo" >Pão Doce</span>
<span class="tipo" >Sem coco</span>
<span class="tipo" >Pão Doce</span>
<span class="tipo" >Sem coco</span>
</div>
I want now to show the user only the 2 types of bread he asked for:
Item 1: Coconut Bun, Item 2: Coconut-free Bun.
The DOM
I would return something like:
Item 1: Coconut Bun, Item 2: Coconut Bun, Item 3: Coconut-free Bun, Item 4: Coconut-free Bun coconut
If you use the unique
from @Miguel Angelo’s tip, the "type" will only repeat once, ie:
Item 1: Coconut Bun, Item 2: Coconut Free.
That is, if you have two of the same types of bread, it will only show either all or only 1, but I want you to show only one group of each: "Sweet Coconut Bread" and take the repeat "Sweet Coconut Bread" but keep the "Sweet Coconut Cake" and remove again the duplicate "Sweet Coconut Free Bread".
Is there any way to do that?
I didn’t know the
array_unique
, even so it did not work, only displayed 1 occurrence to 7 that has currently on the site.– Cassiano José
As array_map was with a single array, I changed it by passing a function to get only the contents of the element, in string form, before passing to array_unique... see if it now works.
– Miguel Angelo
It worked! Now please read the update, to see if you can also get me this question.
– Cassiano José