Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> $objekt->element-obiektu - przez myślnik nie działa ;/, Jak powinno się odwoływać do elementów zawierających myślnik w nazwie?
Apocalyptiq
post 8.02.2009, 18:08:06
Post #1





Grupa: Zarejestrowani
Postów: 230
Pomógł: 3
Dołączył: 8.01.2008

Ostrzeżenie: (10%)
X----


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ąć?


--------------------
http://estender.net - profesjonalne strony i aplikacje internetowe (Ruby on Rails, Kohana PHP)
Go to the top of the page
+Quote Post
dr_bonzo
post 8.02.2009, 18:11:10
Post #2





Grupa: Przyjaciele php.pl
Postów: 5 724
Pomógł: 259
Dołączył: 13.04.2004
Skąd: N/A

Ostrzeżenie: (0%)
-----


$obiekt->{'element-obiektu'}

badz wsadz go do ArrayObject i potem
$ao = new ArrayObject($obiekt);
$ao['element-obiektu']


--------------------
Nie lubię jednorożców.
Go to the top of the page
+Quote Post
Apocalyptiq
post 8.02.2009, 21:00:14
Post #3





Grupa: Zarejestrowani
Postów: 230
Pomógł: 3
Dołączył: 8.01.2008

Ostrzeżenie: (10%)
X----


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 :-)


--------------------
http://estender.net - profesjonalne strony i aplikacje internetowe (Ruby on Rails, Kohana PHP)
Go to the top of the page
+Quote Post
dr_bonzo
post 8.02.2009, 21:02:10
Post #4





Grupa: Przyjaciele php.pl
Postów: 5 724
Pomógł: 259
Dołączył: 13.04.2004
Skąd: N/A

Ostrzeżenie: (0%)
-----


Masz w manualu pokazane jak sie do nich dobierac.
Po prostu
foreach ( $xml->podelement as $el ) >..


--------------------
Nie lubię jednorożców.
Go to the top of the page
+Quote Post
Apocalyptiq
post 8.02.2009, 21:16:28
Post #5





Grupa: Zarejestrowani
Postów: 230
Pomógł: 3
Dołączył: 8.01.2008

Ostrzeżenie: (10%)
X----


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?


--------------------
http://estender.net - profesjonalne strony i aplikacje internetowe (Ruby on Rails, Kohana PHP)
Go to the top of the page
+Quote Post
dr_bonzo
post 8.02.2009, 21:28:20
Post #6





Grupa: Przyjaciele php.pl
Postów: 5 724
Pomógł: 259
Dołączył: 13.04.2004
Skąd: N/A

Ostrzeżenie: (0%)
-----


Nieczytelny przyklad.


--------------------
Nie lubię jednorożców.
Go to the top of the page
+Quote Post
Apocalyptiq
post 8.02.2009, 21:36:30
Post #7





Grupa: Zarejestrowani
Postów: 230
Pomógł: 3
Dołączył: 8.01.2008

Ostrzeżenie: (10%)
X----


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 :-)


--------------------
http://estender.net - profesjonalne strony i aplikacje internetowe (Ruby on Rails, Kohana PHP)
Go to the top of the page
+Quote Post
marcinpruciak
post 9.02.2009, 16:04:32
Post #8





Grupa: Zarejestrowani
Postów: 161
Pomógł: 9
Dołączył: 14.07.2008

Ostrzeżenie: (0%)
-----


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. ?>


--------------------
Go to the top of the page
+Quote Post
Apocalyptiq
post 10.02.2009, 19:02:53
Post #9





Grupa: Zarejestrowani
Postów: 230
Pomógł: 3
Dołączył: 8.01.2008

Ostrzeżenie: (10%)
X----


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 :-)


--------------------
http://estender.net - profesjonalne strony i aplikacje internetowe (Ruby on Rails, Kohana PHP)
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 14.06.2025 - 18:13