Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [XML][PHP]pobieranie wartość z XML za pomocą xpath
pabito
post
Post #1





Grupa: Zarejestrowani
Postów: 77
Pomógł: 4
Dołączył: 14.05.2013

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


Witam
Mam taką strukturę, w jaki sposób mogę pobrać datę(data_eveniment) tylko dlatego obiektu gdzie cod_eveniment = "OK"

  1. array(5) {
  2. [0]=>
  3. object(SimpleXMLElement)#2 (7) {
  4. ["data_eveniment"]=>
  5. string(19) "2014-06-25T10:37:20"
  6. ["centru"]=>
  7. string(9) "BUCURESTI"
  8. ["cod_eveniment"]=>
  9. string(2) "OK"
  10. ["descriere_eveniment"]=>
  11. string(7) "Livrata"
  12. ["persoana_livrare"]=>
  13. string(16) "renato pesamosca"
  14. ["observatii_livrare"]=>
  15. object(SimpleXMLElement)#7 (0) {
  16. }
  17. ["motiv"]=>
  18. object(SimpleXMLElement)#8 (0) {
  19. }
  20. }
  21. [1]=>
  22. object(SimpleXMLElement)#3 (7) {
  23. ["data_eveniment"]=>
  24. string(19) "2014-06-25T08:52:42"
  25. ["centru"]=>
  26. string(9) "BUCURESTI"
  27. ["cod_eveniment"]=>
  28. string(2) "WC"
  29. ["descriere_eveniment"]=>
  30. string(15) "Spre destinatar"
  31. ["persoana_livrare"]=>
  32. object(SimpleXMLElement)#8 (0) {
  33. }
  34. ["observatii_livrare"]=>
  35. object(SimpleXMLElement)#7 (0) {
  36. }
  37. ["motiv"]=>
  38. object(SimpleXMLElement)#9 (0) {
  39. }
  40. }
  41. [2]=>
  42. object(SimpleXMLElement)#4 (7) {
  43. ["data_eveniment"]=>
  44. string(19) "2014-06-25T00:22:37"
  45. ["centru"]=>
  46. string(9) "BUCURESTI"
  47. ["cod_eveniment"]=>
  48. string(2) "AR"
  49. ["descriere_eveniment"]=>
  50. string(26) "Sosit In Statia de Livrare"
  51. ["persoana_livrare"]=>
  52. object(SimpleXMLElement)#9 (0) {
  53. }
  54. ["observatii_livrare"]=>
  55. object(SimpleXMLElement)#7 (0) {
  56. }
  57. ["motiv"]=>
  58. object(SimpleXMLElement)#8 (0) {
  59. }
  60. }
  61. [3]=>
  62. object(SimpleXMLElement)#5 (7) {
  63. ["data_eveniment"]=>
  64. string(19) "2014-06-24T20:05:48"
  65. ["centru"]=>
  66. string(7) "CRAIOVA"
  67. ["cod_eveniment"]=>
  68. string(2) "DF"
  69. ["descriere_eveniment"]=>
  70. string(17) "Plecat din Centru"
  71. ["persoana_livrare"]=>
  72. object(SimpleXMLElement)#8 (0) {
  73. }
  74. ["observatii_livrare"]=>
  75. object(SimpleXMLElement)#7 (0) {
  76. }
  77. ["motiv"]=>
  78. object(SimpleXMLElement)#9 (0) {
  79. }
  80. }
  81. [4]=>
  82. object(SimpleXMLElement)#6 (7) {
  83. ["data_eveniment"]=>
  84. string(19) "2014-06-24T18:31:25"
  85. ["centru"]=>
  86. string(7) "CRAIOVA"
  87. ["cod_eveniment"]=>
  88. string(2) "CI"
  89. ["descriere_eveniment"]=>
  90. string(22) "Receptionat in depozit"
  91. ["persoana_livrare"]=>
  92. object(SimpleXMLElement)#9 (0) {
  93. }
  94. ["observatii_livrare"]=>
  95. object(SimpleXMLElement)#7 (0) {
  96. }
  97. ["motiv"]=>
  98. object(SimpleXMLElement)#8 (0) {
  99. }
  100. }
  101. }


Ten post edytował pabito 28.06.2014, 14:59:31
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 11)
johny_s
post
Post #2





Grupa: Zarejestrowani
Postów: 594
Pomógł: 122
Dołączył: 17.07.2005
Skąd: P-na

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


i naprawdę myślisz, że tą sieczkę ktoś będzie czytał
Go to the top of the page
+Quote Post
pabito
post
Post #3





Grupa: Zarejestrowani
Postów: 77
Pomógł: 4
Dołączył: 14.05.2013

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


poprawione
Go to the top of the page
+Quote Post
tzm
post
Post #4





Grupa: Zarejestrowani
Postów: 675
Pomógł: 58
Dołączył: 17.12.2013

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


  1. for($i = 0; $i < count($x); $i++){
  2. if($x[$i]["cod_eveniment"] == "OK"){echo $x[$i]["data_eveniment"];}
  3. }
Go to the top of the page
+Quote Post
johny_s
post
Post #5





Grupa: Zarejestrowani
Postów: 594
Pomógł: 122
Dołączył: 17.07.2005
Skąd: P-na

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


no to robisz pętelke po tablicy sprawdzasz obiekt
  1. foreach($xml as $x) {
  2. if($x->cod_eveniment == 'OK') echo $x->data_eveniment;
  3. }

możesz date zapisać do zmiennej czy cokolwiek z nią potrzebujesz zrobić
Go to the top of the page
+Quote Post
tzm
post
Post #6





Grupa: Zarejestrowani
Postów: 675
Pomógł: 58
Dołączył: 17.12.2013

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


FOR jest wydajniejsze wg http://www.phpbench.com/
Go to the top of the page
+Quote Post
Pyton_000
post
Post #7





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

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


A nie możesz wkleić kawałek XML questionmark.gif
Go to the top of the page
+Quote Post
johny_s
post
Post #8





Grupa: Zarejestrowani
Postów: 594
Pomógł: 122
Dołączył: 17.07.2005
Skąd: P-na

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


Cytat(mitorski @ 28.06.2014, 14:14:24 ) *
FOR jest wydajniejsze wg http://www.phpbench.com/

i co będziemy się bić o ułamek sekundy?
to dlaczego count wrzucasz do środka for a stringi do podwójnych apostrofów?
Go to the top of the page
+Quote Post
tzm
post
Post #9





Grupa: Zarejestrowani
Postów: 675
Pomógł: 58
Dołączył: 17.12.2013

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


Zawsze chyba lepiej użyć wydajniejszej metody, bić się nie bije. Dyskutuję.. od tego jest forum.
A gdzie mam wrzucisz count? coś musi zliczyć elementy tablicy a bez sensu jest pisanie czegoś w stylu:

  1. $x = count($array);
  2. for($i = 0;$i <$x;$i++){}


To samo a mniej kodu jak zapisuję count($x).

Jak nie podajesz stringów w "" to wywala notice:

  1. Notice: Use of undefined constant data_eveniment - assumed 'data_eveniment' in C:\xampp\htdocs\encom\arr1.php on line 11

Go to the top of the page
+Quote Post
Turson
post
Post #10





Grupa: Zarejestrowani
Postów: 4 291
Pomógł: 829
Dołączył: 14.02.2009
Skąd: łódź

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


Wystarczy użyć 'index' facepalmxd.gif
Go to the top of the page
+Quote Post
johny_s
post
Post #11





Grupa: Zarejestrowani
Postów: 594
Pomógł: 122
Dołączył: 17.07.2005
Skąd: P-na

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


Tak, dokładnie tak, masz zliczyć wcześniej, przeczytaj drugi benchmark z linka który podałeś wcześniej

I nie napisałem, że nie masz używać apostrofów, bo wtedy php traktuje to jak stałe,
tam gdzie nie wymaga to parsowania stringów (np. dopisywanie zmiennych w środku tekstu) pojedyńcze apostrofy są szybsze

tylko że, to wszystko to ułamki sekund, jedno głupie echo zajmie więcej czasu niż przejechanie całej pętli (oczywiście nie mającej miliona elementów)
Go to the top of the page
+Quote Post
Pyton_000
post
Post #12





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

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


Cytat(mitorski @ 28.06.2014, 16:31:23 ) *
Zawsze chyba lepiej użyć wydajniejszej metody, bić się nie bije. Dyskutuję.. od tego jest forum.
A gdzie mam wrzucisz count? coś musi zliczyć elementy tablicy a bez sensu jest pisanie czegoś w stylu:

  1. $x = count($array);
  2. for($i = 0;$i <$x;$i++){}


To samo a mniej kodu jak zapisuję count($x).

Jak nie podajesz stringów w "" to wywala notice:

  1. Notice: Use of undefined constant data_eveniment - assumed 'data_eveniment' in C:\xampp\htdocs\encom\arr1.php on line 11


Hehe i pisze to człowiek który posiłkuje się stroną która jest czysto informacyjna. Ba ale zobaczywszy jeden kawałek jaki to for jest szybszy od foreach nie doczytawszy do końca wsadza babol który powoduje spooory narzut wydajności wink.gif Zresztą kolega wyżej już to skomentował.

Jak już chcesz używać for to tak:
  1. for($i=0, $x=count($cos); $i<$x; $x++)
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 Aktualny czas: 21.08.2025 - 18:48