Добрый день, друзья. Подскажите пожалуйста как правильнее парсить данные и как организовать цикл для вывода данных парсинга. Я обращаюсь на сайт, мне приходит ответ такого рода, если парсить в XML: Код (Text): <result> <status>1</status> <num_results>100</num_results> <total_results>500</total_results> <results_remaining>400</results_remaining> <matches> <match> <match_id>1487575980</match_id> <match_seq_num>1334122787</match_seq_num> <start_time>1432118783</start_time> <lobby_type>7</lobby_type> <radiant_team_id>0</radiant_team_id> <dire_team_id>0</dire_team_id> <players> <player> <account_id>4294967295</account_id> <player_slot>0</player_slot> <hero_id>26</hero_id> </player> <player> <account_id>22032060</account_id> <player_slot>1</player_slot> <hero_id>22</hero_id> </player> <player> <account_id>24802150</account_id> <player_slot>2</player_slot> <hero_id>8</hero_id> </player> <player> <account_id>4294967295</account_id> <player_slot>3</player_slot> <hero_id>23</hero_id> </player> <player> <account_id>4294967295</account_id> <player_slot>4</player_slot> <hero_id>35</hero_id> </player> <player> <account_id>4294967295</account_id> <player_slot>128</player_slot> <hero_id>25</hero_id> </player> <player> <account_id>4294967295</account_id> <player_slot>129</player_slot> <hero_id>73</hero_id> </player> <player> <account_id>4294967295</account_id> <player_slot>130</player_slot> <hero_id>19</hero_id> </player> <player> <account_id>4294967295</account_id> <player_slot>131</player_slot> <hero_id>88</hero_id> </player> <player> <account_id>138657453</account_id> <player_slot>132</player_slot> <hero_id>46</hero_id> </player> </players> </match> <match> <match_id>1486500565</match_id> <match_seq_num>1333203789</match_seq_num> <start_time>1432069365</start_time> <lobby_type>7</lobby_type> <radiant_team_id>0</radiant_team_id> <dire_team_id>0</dire_team_id> <players> <player> <account_id>4294967295</account_id> <player_slot>0</player_slot> <hero_id>84</hero_id> </player> <player> <account_id>4294967295</account_id> <player_slot>1</player_slot> <hero_id>29</hero_id> </player> <player> <account_id>4294967295</account_id> <player_slot>2</player_slot> <hero_id>93</hero_id> </player> <player> <account_id>97739429</account_id> <player_slot>3</player_slot> <hero_id>12</hero_id> </player> <player> <account_id>157387016</account_id> <player_slot>4</player_slot> <hero_id>76</hero_id> </player> <player> <account_id>81863485</account_id> <player_slot>128</player_slot> <hero_id>33</hero_id> </player> <player> <account_id>48311512</account_id> <player_slot>129</player_slot> <hero_id>37</hero_id> </player> <player> <account_id>67701328</account_id> <player_slot>130</player_slot> <hero_id>2</hero_id> </player> <player> <account_id>22032060</account_id> <player_slot>131</player_slot> <hero_id>22</hero_id> </player> <player> <account_id>24802150</account_id> <player_slot>132</player_slot> <hero_id>8</hero_id> </player> </players> </match> <match> <match_id>1485822121</match_id> <match_seq_num>1332607189</match_seq_num> <start_time>1432046719</start_time> <lobby_type>7</lobby_type> <radiant_team_id>0</radiant_team_id> <dire_team_id>0</dire_team_id> <players> <player> <account_id>4294967295</account_id> <player_slot>0</player_slot> <hero_id>71</hero_id> </player> <player> <account_id>4294967295</account_id> <player_slot>1</player_slot> <hero_id>67</hero_id> </player> <player> <account_id>103810932</account_id> <player_slot>2</player_slot> <hero_id>79</hero_id> </player> <player> <account_id>4294967295</account_id> <player_slot>3</player_slot> <hero_id>106</hero_id> </player> <player> <account_id>4294967295</account_id> <player_slot>4</player_slot> <hero_id>74</hero_id> </player> <player> <account_id>22032060</account_id> <player_slot>128</player_slot> <hero_id>22</hero_id> </player> <player> <account_id>24802150</account_id> <player_slot>129</player_slot> <hero_id>5</hero_id> </player> <player> <account_id>128966659</account_id> <player_slot>130</player_slot> <hero_id>44</hero_id> </player> <player> <account_id>4294967295</account_id> <player_slot>131</player_slot> <hero_id>7</hero_id> </player> <player> <account_id>4294967295</account_id> <player_slot>132</player_slot> <hero_id>61</hero_id> </player> </players> </match> </matches> И так далее, таких <match> много. Подскажите как это правильно обработать ? Вывести в итоге каждый матч (<match>) в отдельной строке. И второй вопрос, как можно "заходить" во внутрь каждого матча (<match>) и вытаскивать данные оттуда в одном файле? Чтобы в одном файле я отображал список этих самых матчей и некоторую индивидуальную информацию уже конкретно по каждому матчу ? Вот ответ сервера, если парсить через JSON Код (Text): { "result": { "status": 1, "num_results": 100, "total_results": 500, "results_remaining": 400, "matches": [ { "match_id": 1487575980, "match_seq_num": 1334122787, "start_time": 1432118783, "lobby_type": 7, "radiant_team_id": 0, "dire_team_id": 0, "players": [ { "account_id": 4294967295, "player_slot": 0, "hero_id": 26 }, { "account_id": 22032060, "player_slot": 1, "hero_id": 22 }, { "account_id": 24802150, "player_slot": 2, "hero_id": 8 }, { "account_id": 4294967295, "player_slot": 3, "hero_id": 23 }, { "account_id": 4294967295, "player_slot": 4, "hero_id": 35 }, { "account_id": 4294967295, "player_slot": 128, "hero_id": 25 }, { "account_id": 4294967295, "player_slot": 129, "hero_id": 73 }, { "account_id": 4294967295, "player_slot": 130, "hero_id": 19 }, { "account_id": 4294967295, "player_slot": 131, "hero_id": 88 }, { "account_id": 138657453, "player_slot": 132, "hero_id": 46 } ] }, { "match_id": 1486500565, "match_seq_num": 1333203789, "start_time": 1432069365, "lobby_type": 7, "radiant_team_id": 0, "dire_team_id": 0, "players": [ { "account_id": 4294967295, "player_slot": 0, "hero_id": 84 }, { "account_id": 4294967295, "player_slot": 1, "hero_id": 29 }, { "account_id": 4294967295, "player_slot": 2, "hero_id": 93 }, { "account_id": 97739429, "player_slot": 3, "hero_id": 12 }, { "account_id": 157387016, "player_slot": 4, "hero_id": 76 }, { "account_id": 81863485, "player_slot": 128, "hero_id": 33 }, { "account_id": 48311512, "player_slot": 129, "hero_id": 37 }, { "account_id": 67701328, "player_slot": 130, "hero_id": 2 }, { "account_id": 22032060, "player_slot": 131, "hero_id": 22 }, { "account_id": 24802150, "player_slot": 132, "hero_id": 8 } ] }, Как будет правильнее и удобнее парсить ? Заранее большое спасибо Вам за ответы!
..... в начале надо пропустить json-ответ через jsone_decode, а потом уже "выгребать" всё... всё просто
я понимаю, что за меня делать никто не будет, но ведь это не пример) вы говорите сейчас на не понятном для меня языке Тащу данные я таким образом: Код (PHP): $url = file_get_contents("http://site.com); $content = json_decode($url, true); В массиве $content хранится весь ответа от сервера. А как его мне красиво в читабельный для пользователя вид привести ?
раз вложенность не может быть разная то - рекурсивно... соответственно, нужно сделать разбор этого "$content" внутри функции и плюс оборачивать html тегами для красоты пример рекурсии Код (Text): function recursion($a) { if ($a < 20) { echo "$a\n"; recursion($a + 1); } } ?>
Спасибо, подскажите пожалуйста, как правильно цикл (вывод всех данных) по массиву соорудить ? Массив многомерный, такой код у меня не работает: Код (PHP): $url_history = file_get_contents("https://site.com"); $match_history = json_decode($url_history, true); foreach ($match_history as &$mh) { echo $mh[]; }
если чисто для проверки объекта, то будет выглядеть так Код (Text): foreach ($match_history as $mh) { echo '=======================<pre>'; print_r( $mh ); echo '</pre>-------------------------------'; }