php - 使用 PHP 数组将 JSON 内容保存在 Mysql 中 [调试]

标签 php mysql arrays json

我正在尝试将 JSON 数据解析到我的数据库中。

一切都很顺利,直到嵌套数组为止。

我试图将其保存为 MYSQL(phpMyAdmin) 中的数组,但没有结果。

这是我的 Json 文件:

{"_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"
            }
        ]
    }
]
}

因为有点大,我让你知道它是怎样的。

有主舰。船有标题、id、个人资料图像等。这些都保存到我的数据库中。但在 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

最佳答案

这是一个您试图放入数据库的 php array()。您可以将该部分编码回 json 字符串并将该字符串保存在数据库中。

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

关于php - 使用 PHP 数组将 JSON 内容保存在 Mysql 中 [调试],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44718054/

相关文章:

只有 1 个值的 MySQL 数据类型?

c++ - 使用基本构造函数创建派生对象

c - 通过C中的数组名称和指针访问2D数组元素

php $fopen 错误

php - 为什么.php 页面显示为文本?

php - 我该如何着手创建这种性质的提交门户?

mysql - 我们可以在 MySQL 中使用 IN () 和 NOT IN () 吗?

javascript - Vue JS 使用对象分配但该对象中的数组仍然跟踪更改

java - 在 Java 中解密 PHP AES-256-CFB

php - 发送带有服务器缓存文件的 header