3
    SimpleXMLElement Object
    (
        [deelnemer] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [startnummer] => A1
                        [naam] => Jerry
                        [adres] => Straat 38
                        [postcode] => 0000 MC
                        [woonplaats] => Tilburg
                        [land] => NED
                        [geboortedatum] => 27-02-1988
                        [geslacht] => M
                        [categorie] => Heren
                        [onderdeel] => E. 10 Miles - start 15u00
                    )

                [1] => SimpleXMLElement Object


                    (
                        [startnummer] => A2
                        [naam] => Wesley
                        [adres] => straat 13
                        [postcode] => 0000 AJ
                        [woonplaats] => Tilburg
                        [land] => NED
                        [geboortedatum] => 20-04-1979
                        [geslacht] => M
                        [categorie] => Heren
                        [onderdeel] => E. 10 Miles - start 15u00
                    )

            )
    )

I have this array. I need per 'deelnemer' the individual values. I tried:

echo '<select>';
foreach ($xml as $obj)
{
    foreach ($obj['deelnemer'] as $ob)
    {

            echo '<option value='.$ob['naam'].'>'.$ob['naam'].'</option>';

    }
}
echo '</select>';

But i keep getting error messages. The only thing i can substract atm is 'deelnemer'.

I need a PHP code to create a loop (for each) with a dropdown select with the names of the participants.

René Hoffmann
  • 2,766
  • 2
  • 20
  • 43
Jasper Renema
  • 143
  • 2
  • 10

1 Answers1

4

$xml is not an array but an object, so in order to access its elements use -> instead of [] as explained in php how to access object array

Try this code:

echo '<select>';
foreach ($xml->deelnemer as $ob) {
  echo '<option value="' . $ob->naam . '">' . $ob->naam . '</option>';
}
echo '</select>';
Community
  • 1
  • 1