Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [simpleXML] funkcja obj2array zle dziala
aleksander
post
Post #1





Grupa: Przyjaciele php.pl
Postów: 742
Pomógł: 0
Dołączył: 14.12.2003
Skąd: Gdańsk, Trójmiasto

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


witam,

Na php.net znalazłem funkcję która konwertuje obiekt simplexml do tablicy:
  1. <?php
  2. function recursive_obj2array( $obj )
  3. {
  4. $subject_array = array();
  5.  foreach ((array) $obj as $key => $var)
  6.  {
  7.  if (is_object($var))
  8.  {
  9.  if(count((array) $var) == 0)
  10.  {
  11.  $subject_array[$key] = 'NULL';
  12. }
  13. else
  14. {
  15. recursive_obj2array($var );
  16. }
  17. }
  18. else
  19. {
  20. $subject_array[$key] = $var;
  21. }
  22. }
  23. return $subject_array;
  24. }
  25.  
  26. echo '<pre>';
  27. print_r( recursive_obj2array( simplexml_load_file('xml.xml') ) );
  28. echo '</pre>'; 
  29. ?>
Niestety dla takiego xmlu:
  1. <actions>
  2.    <action>
  3.      <default>true</default>
  4.      <name>main</name>
  5.      <requiredRoles>viewNews</requiredRoles>
  6.      <type>view</type>
  7.      <matches>main</matches>
  8.      <fallback>accessDenied</fallback>
  9.    </action>
  10.  
  11.    <action>
  12.      <name>footer</name>
  13.      <requiredRoles>viewNews</requiredRoles>
  14.      <type>view</type>
  15.      <matches>null</matches>
  16.      <fallback>accessDenied</fallback>
  17.    </action>
  18. </actions>
wychodzi zly kod, bo z obiektem:
Cytat
Array
(
    [action] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [default] => true
                    [name] => main
                    [requiredRoles] => viewNews
                    [type] => view
                    [matches] => main
                    [fallback] => accessDenied
                )

            [1] => SimpleXMLElement Object
                (
                    [name] => footer
                    [requiredRoles] => viewNews
                    [type] => view
                    [matches] => null
                    [fallback] => accessDenied
                )

        )

)
Nie wiem jak to naprawić, bo zamiast obiektu powinna byc kolejna tablica (IMG:http://forum.php.pl/style_emoticons/default/sad.gif) Może mi ktoś pomóc?

Ten post edytował olo 13.01.2005, 18:03:26
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
aleksander
post
Post #2





Grupa: Przyjaciele php.pl
Postów: 742
Pomógł: 0
Dołączył: 14.12.2003
Skąd: Gdańsk, Trójmiasto

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


witam,

doszedłem już czemu to nie działa. Napisałem własny konwerter obj=>arr:
  1. <?php
  2. $obiekty = array();
  3. function xml2array( $obj )
  4. {
  5. global $obiekty;
  6.  
  7. foreach( (array)$obj as $key => $val )
  8. {
  9. if( is_object( $val ) )
  10. {
  11. xml2array( $val );
  12. }/*
  13. elseif( is_array( $val ) )
  14. {
  15. xml2array( $val );
  16. }*/
  17. else
  18. {
  19. $obiekty[$key] = $val;
  20. }
  21. }
  22. }
  23. ?>

teraz dwa listtingi: pierwszy to var_dump obiektu simplexml, drugi to wynik działania funkcji na tym obiekcie:
Cytat
object(SimpleXMLElement)#1 (1) {
  ["action"]=>
  array(2) {
    [0]=>
    object(SimpleXMLElement)#4 (6) {
      ["default"]=>
      string(4) "true"
      ["name"]=>
      string(4) "main"
      ["requiredRoles"]=>
      string(8) "viewNews"
      ["type"]=>
      string(4) "view"
      ["matches"]=>
      string(4) "main"
      ["fallback"]=>
      string(12) "accessDenied"
    }
    [1]=>
    object(SimpleXMLElement)#5 (5) {
      ["name"]=>
      string(6) "footer"
      ["requiredRoles"]=>
      string(8) "viewNews"
      ["type"]=>
      string(4) "view"
      ["matches"]=>
      string(4) "null"
      ["fallback"]=>
      string(12) "accessDenied"
    }
  }
}
Cytat
Array
(
    [action] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [default] => true
                    [name] => main
                    [requiredRoles] => viewNews
                    [type] => view
                    [matches] => main
                    [fallback] => accessDenied
                )

            [1] => SimpleXMLElement Object
                (
                    [name] => footer
                    [requiredRoles] => viewNews
                    [type] => view
                    [matches] => null
                    [fallback] => accessDenied
                )

        )

)
Podczas pierwszej rekurencji funkcja widzi TYLKO tablice, więc nie wchodzi do tej tabicy, dlatego w srodku pozostają obiekty. Zakomentowałem sposób w jaki chciałem to ominąc, niestety on nie działa. TerazCzy ktoś wie jak to naprawić (IMG:http://forum.php.pl/style_emoticons/default/smile.gif) (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif)
Go to the top of the page
+Quote Post

Posty w temacie


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

 



RSS Aktualny czas: 3.10.2025 - 16:27