2017-06-23 8 views
0

JSONデータをデータベースに解析しようとしています。JSONコンテンツをPHP配列で保存する[デバッグ]

すべてがネストされた配列までうまくいった。

私はそれをMYSQL(phpMyAdmin)の配列として保存しようとしていますが、結果はありません。私はあなたはそれがどのように知っている、ちょっと大きいので

{"_links": 
{ 
"first": 
    { 
    "href":"https://www.widgety.co.uk/api/ships.json?app_id=e701363eb6259342c9ce&limit=1&page=1&token=087c57b15f6e52098e2ad181db0e11919" 
    } 
}, 
"count":1,"total":788, 
    "ships":[ 
    { 
     "title":"AIDAperla", 
     "id":1052, 
     "profile_image_href":"https://widgety-assets.s3.amazonaws.com/2016/06/06/22/33/28/223/AIDA_Cruises_Logo_1.jpg", 
     "cover_image_href":"https://widgety-assets.s3.amazonaws.com/2017/03/22/00/00/22/739/AIDAperla.jpg", 
     "href":"https://www.widgety.co.uk/api/ships/aidaperla.json", 
     "html_href":"https://www.widgety.co.uk/ships/aidaperla", 
     "ship_class":"", 
     "size":"large", 
     "style":"resort", 
     "celebrity_ties":"<p>Tim Malzer- German Celebrity Chef</p>", 
     "gratuities":"<p>All gratuities are included in the Cruise fare.</p>", 
     "ship_facts":{ 

      "launch_year":"2017", 
      "refit_year":"", 
      "language":"de+en", 
      "gross_tonnage":124500, 
      "length":300.0, 
      "width":37.6, 
      "currency":"EUR", 
      "speed":22,"capacity":3300, 
      "crew_count":900, 
      "deck_count":16, 
      "cabin_count":1643, 
      "large_cabin_count":null, 
      "wheelchair_cabin_count":null 

     }, 
     "dining":{ 
      "intro":"<p>At 13 bars the bar team will spoil you with cocktails, wines, beer and champagne.</p><p>In Café Mare (Deck 7) they serve coffee, tea and chocolate specialties, in the wine cellar (Deck 8) wines from around the world and in the Bar 5th Element (deck 14) can families relaxed end the day. In the exclusive Spray Bar (deck 6) , you can go directly on the bow, where you can feel the spray, the spray, on your face and enjoy your champagne. In the Nightfly (Deck 6) you can enjoy exclusive entertainment and in the discotheque D6 (Deck 6) dance night owls dance until dawn.</p><p>All opening hours and special offers can be found in the on-board newspaper. </p><p></p><ul><li>Scharfe Ecke (Sharp Corner, complimentary fastfood dining venue)<br></li><li>Rossini (specialty, dinner-only restaurant at surcharge, reservations required; offers waiter service and 6-course a-la-carte menu)<br></li><li>Market Restaurant (complimentary main dining room; self-service buffet restaurant with a variety of food stations and a small terrace)<br></li><li>Bella Donna (Italian cuisine restaurant, complimentary, buffet-style dining, open 24 hours)<br></li><li>Ristorante Casa Nova (Mediterranean cuisine restaurant at surcharge)<br></li><li>Brasserie French Kiss (French cuisine restaurant, reservations-only, at surcharge)<br></li><li>Weite Welt Restaurant (Wide World complimentary buffet-style restaurant)<br></li><li>Buffalo Steakhouse (American fare restaurant with a la carte menu; reservations required; at surcharge)<br></li><li>Sushi Bar (Japanese restaurant at surcharge, reservations are required)<br></li><li>Fuego Restaurant (new family restaurant, complimentary, with a kids buffet)<br></li><li>East Restaurant (Asian cuisine, buffet-style complimentary restaurant).<br></li></ul><p></p>", 
      "video":"" 
     }, 
     "enrichment":{ 
      "intro":"<p>More information coming soon.</p>", 
      "video":"" 
     }, 
     "entertainment":{ 
      "intro":"<p>Your chocolate, your topping, your ice cream! In the first MAGNUM Pleasure Store on the sea, you can create your personal favourite ice cream - or try every day a different combination.</p><p>An exclusive stay, guests enjoy the spray bar by Möet &amp; Chandon : The luxurious champagne bar with an outdoor area at the bow just above the waves is certainly the new gathering place. The famous Sundowner served in the Lanai Bar at the rear with a particularly romantic view of the sunset.</p><p>Other day: 14 cafes and bars as well as the AIDA Lounge for relaxed meetings with friends or celebrations</p><p></p><ul><li>Live music as a stylish accompaniment in selected bars<br></li><li>A touch of Casino Royale in the Casino Bar<br></li><li>Thirst quencher for adventurers in . 5 Element directly on the Activity deck<br></li></ul><p></p>", 
      "video":"" 
     }, 
     "health_and_fitness":{ 
      "intro":"<p>On board you'll find a range of health and fitness facilities including a state-of-the-art fitness centre, a spa, jogging track, sports area, pools, jacuzzis and a waterpark.</p>", 
      "video":"" 
     }, 
     "kids_and_teens":{ 
      "intro":"<p>There are a range of activities for kids &amp; teens onboard AIDAperla.</p>", 
      "video":"" 
     }, 
     "useful_to_know":{ 
      "video":"" 
     }, 
     "operator":{ 
      "name":"AIDA Cruises", 
      "profile_image_href":"https://widgety-assets.s3.amazonaws.com/2016/06/06/22/33/28/223/AIDA_Cruises_Logo_1.jpg", 
      "cover_image_href":"https://widgety-assets.s3.amazonaws.com/2016/06/07/16/16/35/361/AIDAblu.jpg"}, 
      "accomodation_types":[ 
       { 
       "name":"Suite", 
       "description":"<p>Suite with separate sleeping area, 2 bathrooms and a walk-in closet. All cabins are air-conditioned , carpeted and have shower/WC, satellite TV, radio, safe, second pillow top and direct dial telephone. The 220 V connection in the bathroom is intended for the use of a razor. A hairdryer is in the desk drawer. </p><p></p><ul><li>Access to the AIDA Lounge and Patio deck<br></li><li>Concierge Service<br></li><li>Fresh fruit, chocolates and champagne to welcome<br></li><li>3-course menu on the welcome evening in the à la carte restaurant<br></li><li>Breakfast is served in the à la carte restaurant, in the AIDA lounge or on the cabin <br></li><li>Free use of the organic spa on the day of departure<br></li><li>Bathrobes and slippers for adults and children<br></li><li>Amenities such as bath tub and body emulsion in the bathroom<br></li><li>Turn Down Service<br></li><li>Luggage scale, chair, coat-rack and umbrella as comfort extras<br></li><li>Your favourite pillow from our pillow menu for heavenly sleep comfort<br></li></ul><p></p>", 
       "stats":"<p></p><ul><li>419 - 1237 sq. ft.<br></li><li>Private sun deck: 118 - 775 sq. ft.<br></li><li>Sleeps 4<br></li></ul><p></p>" 
      }, { 
       "name":"Veranda Comfort Cabin", 
       "description":"<p>All cabins are air-conditioned , carpeted and have shower/WC, satellite TV, radio, safe, second pillow top and direct dial telephone. The 220 V connection in the bathroom is intended for the use of a razor. A hairdryer is in the desk drawer. </p>", 
       "stats":"<p></p><ul><li>258 - 398 sq. ft.<br></li><li>Veranda: 43 - 172 sq. ft.<br></li><li>Sleeps 3 - 4 depending on category<br></li></ul><p></p>" 
      }, { 
       "name":"Veranda Cabin", 
       "description":"<p>All cabins are air-conditioned , carpeted and have shower/WC, satellite TV, radio, safe, second pillow top and direct dial telephone. The 220 V connection in the bathroom is intended for the use of a razor. A hairdryer is in the desk drawer. </p>", 
       "stats":"<p></p><ul><li>226 - 290 sq. ft.<br></li><li>Veranda: 43 - 139 sq. ft.<br></li><li>Sleeps 2 - 4 depending on category<br></li></ul><p></p>" 
      }, { 
       "name":"Panorama Cabin","description":"<p>Exposed position on the patio deck at the bow of deck 16.</p><p>All cabins are air-conditioned , carpeted and have shower/WC, satellite TV, radio, safe, second pillow top and direct dial telephone. The 220 V connection in the bathroom is intended for the use of a razor. A hairdryer is in the desk drawer. <br></p>", 
       "stats":"<p></p><ul><li>258 - 290 sq. ft.<br></li><li>Veranda: 75 sq. ft.<br></li><li>Sleeps 2 - 3 <br></li></ul><p></p>" 
      }, { 
       "name":"Lanai Cabin", 
       "description":"<p>Includes winter garden and porch.</p><p>Mostly with 2 bathrooms.</p><p>All cabins are air-conditioned , carpeted and have shower/WC, satellite TV, radio, safe, second pillow top and direct dial telephone. The 220 V connection in the bathroom is intended for the use of a razor. A hairdryer is in the desk drawer. <br></p>", 
       "stats":"<p></p><ul><li>322 - 365 sq. ft.<br></li><li>Winter garden: 64 sq. ft.<br></li><li>Veranda: 64 sq. ft.<br></li><li>Sleeps 4<br></li></ul><p></p>" 
      }, {"name":"Sea View Cabin","description":"<p>All cabins are air-conditioned , carpeted and have shower/WC, satellite TV, radio, safe, second pillow top and direct dial telephone. The 220 V connection in the bathroom is intended for the use of a razor. A hairdryer is in the desk drawer. <br></p>", 
       "stats":"<p></p><ul><li>236 - 247 sq. ft.<br></li><li>Sleeps 3 - 4 depending on category.<br></li></ul><p></p>" 
      }, { 
       "name":"Inside Cabin","description":"<p>All cabins are air-conditioned , carpeted and have shower/WC, satellite TV, radio, safe, second pillow top and direct dial telephone. The 220 V connection in the bathroom is intended for the use of a razor. A hairdryer is in the desk drawer. </p>", 
       "stats":"<p></p><ul><li>172 sq. ft.<br></li><li>Sleeps 2 - 4<br></li><li>Partly separated beds<br></li></ul><p></p>" 
      } 
      ], 
      "deckplans":[ 
       { 
       "name":"Deck 18", 
       "description":"<ul><li>1. FKK Area<br></li><li>2. Start Racer<br></li></ul>" 
      }, { 
       "name":"Deck 17", 
       "description":"<p></p><ul><li>3. Sun Deck<br></li></ul><p></p>" 
      }, { 
       "name":"Deck 16", 
       "description":"<ul><li>4. Patio Deck<br></li><li>5. Patio Bar<br></li><li>6. Beach Club<br></li><li>7. Sports Deck/Ice Rink in Winter<br></li><li>8. Climing Wall<br></li><li>Suites</li><li>Cabins</li></ul>" 
      }, { 
       "name":"Deck 15", 
       "description":"<ul><li>3. Sun Deck<br></li><li>6. Beach Club<br></li><li>9. TV Studio<br></li><li>10. AIDA Bar<br></li><li>11. Pool Bar<br></li><li>12. Minigolf<br></li><li>13. Teens Lounge Wave Club<br></li><li>14. Ziel Racer<br></li><li>15. Four Elements Water Park<br></li><li>16. Jogging Track<br></li><li>17. Skywalk<br></li><li>Suites</li><li>Cabins</li></ul>" 
      }, { 
       "name":"Deck 14", 
       "description":"<ul><li>15. Four Elements Water Park<br></li><li>18. Mini Club<br></li><li>19. Fuego Restaurant<br></li><li>20. Kids Club<br></li><li>21. 5. Element<br></li><li>Cabins</li></ul>" 
      }, { 
       "name":"Deck 12", 
       "description":"<p></p><ul><li>Suites<br></li><li>Cabins<br></li></ul><p></p>" 
      }, { 
       "name":"Deck 11", 
       "description":"<p></p><ul><li>Suites<br></li><li>Cabins<br></li></ul><p></p>" 
      }, { 
       "name":"Deck 10", 
       "description":"<p></p><ul><li>Suites<br></li><li>Cabins<br></li></ul><p></p>" 
      }, { 
       "name":"Deck 9", 
       "description":"<p></p><ul><li>Suites<br></li><li>Cabins<br></li></ul><p></p>" 
      }, { 
       "name":"Deck 8", 
       "description":"<ul><li>3. Outside Deck<br></li><li>3. Smoking Area<br></li><li>22. Organic Spa</li><li>23. AIDA Lounge<br></li><li>24. Sunset Bar<br></li><li>25. Excursion Counter<br></li><li>26. Theatre<br></li><li>27. Sushi Bar<br></li><li>28. Library<br></li><li>29. Rossini Restaurant<br></li><li>30. Buffalo Steak House<br></li><li>31. Lanai Deck with Infinity Pools<br></li></ul>" 
      }, { 
       "name":"Deck 7", 
       "description":"<ul><li>25. Excursion Counter<br></li><li>26. Theatre</li><li>31. Lanai Deck</li><li>32. Spray Bar by Moet &amp; Chandon</li><li>33. Body &amp; Soul Sport</li><li>34. Zen Lounge</li><li>35. Sport Shop with Biking, Golf and Dive Counter</li><li>36. Nail Spa</li><li>37. Perfume Shop</li><li>38. Travel Consultant</li><li>39. AIDA Plaza<br></li><li>40. Hairdresser<br></li><li>41. MAGNUM Pleasure Store<br></li><li>42. Kids Shop<br></li><li>43. Flower Shop<br></li><li>44. AIDA Shop<br></li><li>45. Treasure Chest<br></li><li>46. Cafe Mare<br></li><li>47. Hemingway Lounge<br></li><li>48. TV Room<br></li><li>49. Photo Gallery<br></li><li>50. Art Gallery<br></li><li>51. Cooking Studio<br></li><li>52. French Kiss - Brasserie<br></li><li>53. Brauhaus<br></li><li>54. Wide World Restaurant<br></li><li>55. Lanai Bar<br></li></ul>" 
      }, { 
       "name":"Deck 6", 
       "description":"<p></p><ul><li>3. Outside Deck<br></li><li>3. Smoking Area<br></li><li>26. Theatre<br></li><li>32. Spray Bar by Moet &amp; Chandon</li><li>39. AIDA Plaza<br></li><li>56. Disco D6<br></li><li>57. Nightclub Nightfly<br></li><li>58. Tapas &amp; Bar<br></li><li>60. Premium Jeweller<br></li><li>61. Travel Shop<br></li><li>62. Photo Studio<br></li><li>63. Casino<br></li><li>64. Prima Bar<br></li><li>65. Casino Bar<br></li><li>66. East Restaurant<br></li><li>67. Ristorante Casa Nova<br></li><li>68. Bella Donna Restaurant<br></li><li>69. Market Restaurant<br></li></ul><p></p>" 
      }, { 
       "name":"Deck 5", 
       "description":"<p></p><ul><li>Cabins<br></li></ul><p></p>" 
      }, { 
       "name":"Deck 4", 
       "description":"<p></p><ul><li>25. Excursion Counter<br></li><li>70. Travel Service<br></li><li>71. Reception<br></li><li>72. Washrooms</li><li>Cabins</li></ul><p></p>" 
      }, { 
       "name":"Deck 3", 
       "description":"<p></p><ul><li>73. Activities Station<br></li><li>74. Gangway<br></li><li>75. Pier 3 Market<br></li><li>76. Conference Room<br></li><li>77. Medical Centre<br></li></ul><p></p>" 
      }], 
      "useful_types":[{ 
       "name":"Disabled Facilities", 
       "description":"<p>You should be able to enjoy the most beautiful time of the year as independently as possible. AIDA therefore ask our guests with reduced mobility, hearing or vision as well as other physical impairments to report at the beginning of the journey at the reception. Here, they will be glad to answer your individual questions about the barrier-free holiday at any time. In case of emergency, you will be provided with crew members who are sure to accompany you to your assembly area.</p>" 
      }, { 
       "name":"Smoking Restrictions", 
       "description":"<p>With regard to the health and well-being of guests, all cabins and public areas of our ships are non-smoking areas . For guests who smoke, the cabin balconies as well as designated smoking areas are available on the outside decks.</p>" 
      }, { 
       "name":"Dress Code", 
       "description":"<p>AIDA is holiday with a smile and without any compulsion. They ask you, however, not to visit restaurants and bars in bathing or sportswear and not barefoot. They recommend men wear long trousers in the evening in the restaurant .</p>" 
      }], 
      "cruises":[ 
      { 
       "id": "VVV000111", 
       "name": "Super Cruise", 
       "cruise": "https://www.widgety.co.uk/api/cruises/VVV000111.json" 
      } 
     ] 
    } 
] 
} 

これは私のJSONファイルです。

メイン船があります。 Shipにはtitle、id、profile_imageなどがあります。これは私のデータベースに保存されています。しかしship_factsのようなオプションでは配列{launch_year、width etc ...}があり、データベースに文字列配列で保存されます。

私のPHPコードはこれです:

<?php 

require '../config.php'; 
$string = file_get_contents("newjson11.json"); 
$arr = json_decode($string, true); 

for ($i = 0; $i < 788; $i++) { 


    $title    = $arr['ships'][$i]['title']; 
    $id     = $arr['ships'][$i]['id'];  
    $profile_image  = $arr['ships'][$i]['profile_image_href']; 
    $cover_image   = $arr['ships'][$i]['cover_image_href']; 
    $href     = $arr['ships'][$i]['href']; 
    $html_href   = $arr['ships'][$i]['html_href']; 
    $size     = $arr['ships'][$i]['size']; 
    $style    = $arr['ships'][$i]['style']; 
    $gratuities   = $arr['ships'][$i]['gratuities']; 
    $ship_facts   = $arr['ships'][$i]['ship_facts']; 

    $dining    = $arr['ships'][$i]['dining']; 

    $operator    = $arr['ships'][$i]['operator']; 

    //I tried : 

    // 1. $deckplans = $arr['ships'][$i]['deckplans']['name']; 
    // 2. $deckplans = $arr['ships'][$i]['deckplans'][$i]['name']; 
    // 3. $deckplans = $arr['ships']['deckplans']['name']; 
    // 4. $deckplans = $arr['ships']['deckplans'][$i]['name']; 



    $deckplans   = $arr['ships'][$i]['deckplans']; 

    $cruises    = $arr['ships'][$i]['cruises']; 

    $query = "INSERT INTO ships 
         (title, id, profile_image_href, 
         cover_image_href, href, html_href, 
         size, style, gratuities, 
         ship_facts, dining, deckplans, 
         operator)"; 
    $query .= "VALUES('{$title}', '{$id}', '{$profile_image}', 
         '{$cover_image}', '{$href}', '{$html_href}', 
         '{$size}', '{$style}', '{$gratuities}', 
         '{$ship_facts}', '{$dining}', '{$operator}')"; 

    $dataatodb = mysqli_query($con, $query); 

    if(!$dataatodb){ 
     echo("Error" . mysqli_errno($con)); 
    } else { 
     echo '<h1>Succesfull Data Transfer</h1>'; 
    } 
} 
?> 

はまた、私はデータベースに格納する implode(" ",$arr)を使用しようと explodeエコーすると print_rが、まだ何もありません。

助けてくれる人がいますか?それは保存して私のデータベース enter image description here

+1

json形式のship_factsデータをdbに格納します。使用時のデコードjson – pAsh

+1

'var_dump($ query);'を含めてください – ajreal

+0

_Just a small point:_あなたのINSERTには13個のカラムが含まれていますが、12個の値しか使用していませんか? – RiggsFolly

答えて

1

あなたのデータベースに入れようとしているPHP配列()です。その部分をjson文字列にエンコードして、その文字列をデータベースに保存することができます。

$ship_facts   = json_encode($arr['ships'][$i]['ship_facts']); 
+0

はい@verhie that works ... OMGはそれを信じていません。..ありがとうございます。 – Maria

+0

素晴らしい。私はあなたに良い方法を考えるよう助言したい。 1つの船に複数の物件があるとすると、それらを別のテーブルに格納することができます。ただ一つのテーブルにshipを格納するだけで、他のすべてのプロパティを別の 'プロパティ'テーブル(id、ship_id、property_name、property_value [、sort_id])に格納することができます。その後、必要な数のプロパティを持つことができ、フィルタとしても使用できます(launch_year = 2017のすべてのship_idを取得するだけです) – verhie

1

利用のMySQL 5.7のように見え、JSONデータ型の列に格納し、または5.6または前にあれば、TEXT列に文字列として保存する方法を

厥。

関連する問題