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.
Helenp
  • 143
  • 2
  • 15
  • `print_r($newParadasarray)` shows you what? – u_mulder Nov 05 '18 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 – Helenp Nov 05 '18 at 19:15

2 Answers2

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.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • Thanks, Did not work, and yes correct data is returned – Helenp Nov 05 '18 at 19:38
  • Some more information would be useful. What does `print_r($newParadasarray);` after the extract look like? – Nigel Ren Nov 05 '18 at 19:39
  • 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 – Helenp Nov 05 '18 at 20:21
  • OK no problems, but the output looks exactly as I would expect it to so not sure what your problem was. – Nigel Ren Nov 05 '18 at 20:22
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);
Salman A
  • 262,204
  • 82
  • 430
  • 521
  • That was very interesting, no idea that could be done that way, nice and clean. – Helenp Nov 05 '18 at 20:11
  • 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 – Helenp Nov 05 '18 at 20:12