[PHP] Array opbouwen failed

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

  • vorlox
  • Registratie: Juni 2001
  • Laatst online: 02-02-2022

vorlox

I cna ytpe 300 wrods pre miute

Topicstarter
Beste luitjes,

Ik heb een vraag over het opbouwen van een bepaalde array
ik lees een XML bestand in en krijg vervolgens een array met waarde terug
Nu wil ik een dynamische mapping maken.
Dus ik weet de mapping al maar ik wil kunnen instellen welke waarde ik ophaal

Simpel XML voorbeeld

PHP:
1
2
3
4
5
6
7
8
9
10
<datacontainer>
    <product>
       <ID>1</ID
       <name>Naam</name>
    </product> 
    <product>
       <ID>2</ID
       <name>Naam2</name>
    </product>
</datacontainer>


Nu gebruik ik een stukje zoals

PHP:
1
2
3
foreach($xmlC->obj_data->datacontainer[0]->product as $Data)
{
    echo $Data->name[0]."<br>"; }


dit werkt goed

maar nu probeerde ik dit en kent hij de array niet meer

PHP:
1
2
3
4
5
6
7
$FirstLevel = "datacontainer";
$SecondLevel = "product ";

foreach($xmlC->obj_data->datacontainer[0]->$SecondLevel as $Data)
{

    echo $Data->name[0]."<br>"; }


Kan iemand me vertellen hoe ik die array dan dynamisch opbpuw
ik kan er echt niks fatsoenlijks over vinden
Het rare is dat hij hier van een object array overgaat naar een gewone??

Acties:
  • 0 Henk 'm!

  • drm
  • Registratie: Februari 2001
  • Laatst online: 09-06 13:31

drm

f0pc0dert

PHP:
2
$SecondLevel = "product ";
wellicht dat die spatie daar niet helemaal thuishoort?

Music is the pleasure the human mind experiences from counting without being aware that it is counting
~ Gottfried Leibniz


Acties:
  • 0 Henk 'm!

  • vorlox
  • Registratie: Juni 2001
  • Laatst online: 02-02-2022

vorlox

I cna ytpe 300 wrods pre miute

Topicstarter
Hmm ja nee helaas
het is niet die spatie (

Acties:
  • 0 Henk 'm!

  • Vae Victis
  • Registratie: April 2001
  • Laatst online: 07-09 06:15

Vae Victis

Dark Lord of the Sith

PHP:
1
foreach($xmlC->obj_data->datacontainer[0]->$SecondLevel as $Data)

De $ bij $SecondLevel gaat niet werken.
Je kunt het zo doen: (let vooral op het woordje kunt! code word er niet bepaald overzichtelijker door)
PHP:
1
foreach($xmlC->obj_data->datacontainer[0]->{$SecondLevel} as $Data)


En spatie kun je ook in die string weghalen is niet nodig.

Acties:
  • 0 Henk 'm!

  • drm
  • Registratie: Februari 2001
  • Laatst online: 09-06 13:31

drm

f0pc0dert

Vae Victis:
De $ bij $SecondLevel gaat niet werken.
Toch wel.

PHP:
1
2
3
4
5
6
7
8
9
10
11
<?
class C {
   function C ( $p ) {
      $this->someProperty = $p;
   }
}
$obj = new C ( 'spef' );

$property = 'someProperty';
echo $obj->$property;
?>

Geeft toch echt als output gewoon 'spef'

vorlox -> probeer eens een print_r te doen van de data die je wilt gaan benaderen en kijk eens of de properties die je aan wilt spreken uberhaupt wel bestaan.

Music is the pleasure the human mind experiences from counting without being aware that it is counting
~ Gottfried Leibniz


Acties:
  • 0 Henk 'm!

  • T-Blizzard
  • Registratie: Juni 2001
  • Laatst online: 11:26
<ID>1</ID

mis je daar nie iets :?

[ Voor 21% gewijzigd door T-Blizzard op 08-02-2004 23:26 ]

Pagina: 1