0

I can't manage to sort an array alfabetically. It's an array with cities that I get from an external XML.

The XML looks like this, and it's the node localidad I am trying to sort.

<parada>
    <id>506</id>
    <localidad>
        <![CDATA[ Alvor ]]>
    </localidad>
    <parada>
        <![CDATA[ Alvor Baia Hotel (Bus Stop Alvor Férias) ]]>
    </parada>
    <lat>37.1296</lat>
    <lng>-8.58058</lng>
    <horasalida>05:40</horasalida>
</parada>

The relevant code:

$xml = new SimpleXMLElement($viajes);
foreach ($xml->parada as $excursion) {
  $newParadasarray[] = $excursion->localidad;
}
$newParadasarray = array_unique($newParadasarray);

foreach ($newParadasarray as $parada) {

  if (strpos($parada, 'Almuñecar') !== false)
                  echo '<option value="Almuñecar">Almuñecar</option>';

  if (strpos($parada, 'Benalmádena') !== false)
                  echo '<option value="Benalmádena Costa">Benalmádena Costa</option>';

  if (strpos($parada, 'Estepona') !== false)
                  echo '<option value="Estepona">Estepona</option>';
  etc.
}

I have tried with sort() and array_values().

This is the output of print_r($newParadasarray):

Array (
  [0]  => SimpleXMLElement Object ( [0] => SimpleXMLElement Object ( ) )
  [1]  => SimpleXMLElement Object ( [0] => SimpleXMLElement Object ( ) )
  [2]  => SimpleXMLElement Object ( [0] => SimpleXMLElement Object ( ) )
  [4]  => SimpleXMLElement Object ( [0] => SimpleXMLElement Object ( ) )
  [9]  => SimpleXMLElement Object ( [0] => SimpleXMLElement Object ( ) )
  [14] => SimpleXMLElement Object ( [0] => SimpleXMLElement Object ( ) )
  [20] => etc.
2
  • print_r($newParadasarray) shows you what? Commented Nov 5, 2018 at 19:10
  • It gives like this: Array ( [0] => SimpleXMLElement Object ( [0] => SimpleXMLElement Object ( ) ) [1] => SimpleXMLElement Object ( [0] => SimpleXMLElement Object ( ) ) [2] => SimpleXMLElement Object ( [0] => SimpleXMLElement Object ( ) ) [4] => SimpleXMLElement Object ( [0] => SimpleXMLElement Object ( ) ) [9] => SimpleXMLElement Object ( [0] => SimpleXMLElement Object ( ) ) [14] => SimpleXMLElement Object ( [0] => SimpleXMLElement Object ( ) ) [20] => etc Commented Nov 5, 2018 at 19:15

2 Answers 2

2

The problem is that your assigning a SimpleXMLElement into the array, instead you want the content of the element, so just change the line...

$newParadasarray[] = $excursion->localidad;

to

$newParadasarray[] = trim((string)$excursion->localidad);

The cast (string) takes the text content and trim() removes the extra whitespace around it.

I am assuming that you have multiple <parada> elements, so that $xml->parada is returning the correct data.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, Did not work, and yes correct data is returned
Some more information would be useful. What does print_r($newParadasarray); after the extract look like?
It prints this: Array ( [0] => Torremolinos [1] => Benalmádena [2] => - [4] => Almuñecar [9] => Nerja [14] => Málaga Capital [20] => Torrox [31] => Torre del Mar [57] => Rincon de la Victoria [93] => Cádiz [94] => Fuengirola [119] => Mijas Costa [131] => Marbella [176] => Estepona [192] => Sabinillas ) However Salmans solution below worked fine and I will use that. Thanks a lot
OK no problems, but the output looks exactly as I would expect it to so not sure what your problem was.
1

If you're familiar with DOMDocument you could simply do this:

$doc = new DOMDocument();
$doc->loadXML($xml);
$array = array();

foreach($doc->getElementsByTagName("localidad") as $localidad) {
    $array[] = trim($localidad->nodeValue);
}

$array = array_unique($array);
sort($array);

2 Comments

That was very interesting, no idea that could be done that way, nice and clean.
Finally managed to get it work, with some changes,this is the code: $doc = new DOMDocument(); $doc->loadXML($viajes); $newParadasarray = array(); $nodelist = $doc->getElementsByTagName("localidad"); foreach($nodelist as $localidad) { $newParadasarray[] = $localidad->nodeValue; } $newParadasarray = array_unique($newParadasarray); sort($newParadasarray); foreach ($newParadasarray as $parada) { Thanks, again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.