Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: $objekt->element-obiektu - przez myślnik nie działa ;/
Forum PHP.pl > Forum > PHP
Apocalyptiq
Parsując dokument XML natknąłem się na element zawierający w nazwie myślnik. Gdy zapiszę to w PHP tak:
  1. <?php
  2. $objekt->element-obiektu
  3. ?>

Otrzymuje błąd:
Cytat
Parse error: syntax error, unexpected '-', expecting ',' or ')'

Jak to można ominąć?
dr_bonzo
$obiekt->{'element-obiektu'}

badz wsadz go do ArrayObject i potem
$ao = new ArrayObject($obiekt);
$ao['element-obiektu']
Apocalyptiq
Dzięki, działa! :-)

Tylko teraz mam następny problem - w tym pliku XML mam kilka elementów o takiej samej nazwie - tj. w danym elemencie jest sześć elementów o takiej samej nazwie. Jak pobiorę je normalnie $element->podelement, pobiera mi tylko ostatni. Jak mogę pobrać je wszystkie? XML parsuje poprzez simplexml.

Jak nie da rady nic z tym, to najwyżej, tak jak radził bonzo, przerobie ten obiekt na tablice :-)
dr_bonzo
Masz w manualu pokazane jak sie do nich dobierac.
Po prostu
foreach ( $xml->podelement as $el ) >..
Apocalyptiq
I tak właśnie robię. Mam taki obiekt:
Cytat
SimpleXMLElement Object ( [staff-type] => alternate [staff-lines] => 6 [staff-tuning] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [line] => 1 ) [tuning-step] => E [tuning-octave] => 4 ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [line] => 2 ) [tuning-step] => B [tuning-octave] => 3 ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [line] => 3 ) [tuning-step] => G [tuning-octave] => 3 ) [3] => SimpleXMLElement Object ( [@attributes] => Array ( [line] => 4 ) [tuning-step] => D [tuning-octave] => 3 ) [4] => SimpleXMLElement Object ( [@attributes] => Array ( [line] => 5 ) [tuning-step] => A [tuning-octave] => 2 ) [5] => SimpleXMLElement Object ( [@attributes] => Array ( [line] => 6 ) [tuning-step] => D [tuning-octave] => 2 ) ) )

I chce się dobrać do tej tablicy "staff-tuning". Jeżeli zrobię poprzez $obiekt->{'staff-tuning'}, pobiera mi tylko ostatni element. Co robię nie tak?
dr_bonzo
Nieczytelny przyklad.
Apocalyptiq
Cytat
SimpleXMLElement Object (
[staff-type] => alternate
[staff-lines] => 6
[staff-tuning] => Array (
[0] => SimpleXMLElement Object ( [@attributes] => Array ( [line] => 1 ) [tuning-step] => E [tuning-octave] => 4 )
[1] => SimpleXMLElement Object ( [@attributes] => Array ( [line] => 2 ) [tuning-step] => B [tuning-octave] => 3 )
[2] => SimpleXMLElement Object ( [@attributes] => Array ( [line] => 3 ) [tuning-step] => G [tuning-octave] => 3 )
[3] => SimpleXMLElement Object ( [@attributes] => Array ( [line] => 4 ) [tuning-step] => D [tuning-octave] => 3 )
[4] => SimpleXMLElement Object ( [@attributes] => Array ( [line] => 5 ) [tuning-step] => A [tuning-octave] => 2 )
[5] => SimpleXMLElement Object ( [@attributes] => Array ( [line] => 6 ) [tuning-step] => D [tuning-octave] => 2 )
)
)

o, teraz to czytelniej wygląda :-)
marcinpruciak
Musisz zrobić pętle w pętli. Podam Ci przykład z książki:
  1. <?php
  2. $xml = simplexml_load_file('books2.xml');
  3.        foreach($xml->ksiazka as $book) {
  4.            echo "<h2>$book->tytul";
  5.                if(isset($book->tytul['wydanie'])) {
  6.                    echo "(Wydanie {$book->tytul['wydanie']})";
  7.                }
  8.            echo "</h2><p>\n";
  9.            foreach($book->autor as $author) {
  10.                echo "Autor: $author<br />\n";
  11.            }
  12.            echo "Wydano: $book->rok <br />\n";
  13.            if(isset($book->stron)) {
  14.                echo "$book->stron stron<br />\n";
  15.            }
  16.            if(isset($book->rozdzial)) {
  17.                echo '<ul>';
  18.                foreach($book->rozdzial as $chapter) {
  19.                    echo '<li>';
  20.                    if(isset($chapter['numer'])) {
  21.                        echo "Rozdział {$chapter['numer']}:\n";
  22.                    }
  23.                    echo $chapter;
  24.                    if(isset($chapter['stron'])) {
  25.                        echo "({$chapter['stron']} stron)\n";
  26.                    }
  27.                    echo '</li>';
  28.                }
  29.                echo '</ul>';
  30.            }
  31.            echo "</p>\n";                    
  32.        }
  33. ?>
Apocalyptiq
marcinpruciak -> robiłem to właśnie tak, jak przedstawiłeś

Już działa, błąd tkwił w czym innym - próbowałem jako klucze tablicy dawać wynik getAttribute - a to zwraca obiekt :-) Po przekonwertowaniu tego obiketu-atrybutu na int wszystko ładnie działa :-)
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.