如何将json多维数组数据插入数据库

时间:2016-02-17 07:32:10

标签: php

Array
(
    [catalog] => Array
        (
            [book] => Array
                (
                    [0] => Array
                        (
                            [-id] => bk101
                            [author] => Gambardella, Matthew
                            [title] => XML Developer's Guide
                            [genre] => Computer
                            [price] => 44.95
                            [publish_date] => 2000-10-01
                            [description] => An in-depth look at
                        )

                    [1] => Array
                        (
                            [-id] => bk102
                            [author] => Ralls, Kim
                            [title] => Midnight Rain
                            [genre] => Fantasy
                            [price] => 5.95
                            [publish_date] => 2000-12-16
                            [description] => A former arc
                        )

                    [2] => Array
                        (
                            [-id] => bk103
                            [author] => Corets, Eva
                            [title] => Maeve Ascendant
                            [genre] => Fantasy
                            [price] => 5.95
                            [publish_date] => 2000-11-17
                            [description] => A former arc
                        )

                    [3] => Array
                        (
                            [-id] => bk104
                            [author] => Corets, Eva
                            [title] => Oberon's Legacy
                            [genre] => Fantasy
                            [price] => 5.95
                            [publish_date] => 2001-03-10
                            [description] => A former arc
                        )

                    [4] => Array
                        (
                            [-id] => bk105
                            [author] => Corets, Eva
                            [title] => The Sundered Grail
                            [genre] => Fantasy
                            [price] => 5.95
                            [publish_date] => 2001-09-10
                            [description] => A former arc
                        )

                    [5] => Array
                        (
                            [-id] => bk106
                            [author] => Randall, Cynthia
                            [title] => Lover Birds
                            [genre] => Romance
                            [price] => 4.95
                            [publish_date] => 2000-09-02
                            [description] => A former arc
                        )

                    [6] => Array
                        (
                            [-id] => bk107
                            [author] => Thurman, Paula
                            [title] => Splish Splash
                            [genre] => Romance
                            [price] => 4.95
                            [publish_date] => 2000-11-02
                            [description] => A former arc
                        )

                    [7] => Array
                        (
                            [-id] => bk108
                            [author] => Knorr, Stefan
                            [title] => Creepy Crawlies
                            [genre] => Horror
                            [price] => 4.95
                            [publish_date] => 2000-12-06
                            [description] => A former arc
                        )

                    [8] => Array
                        (
                            [-id] => bk109
                            [author] => Kress, Peter
                            [title] => Paradox Lost
                            [genre] => Science Fiction
                            [price] => 6.95
                            [publish_date] => 2000-11-02
                            [description] => A former arc
                        )

                    [9] => Array
                        (
                            [-id] => bk110
                            [author] => O'Brien, Tim
                            [title] => Microsoft .NET: The Programming Bible
                            [genre] => Computer
                            [price] => 36.95
                            [publish_date] => 2000-12-09
                            [description] => A former arc
                        )

                    [10] => Array
                        (
                            [-id] => bk111
                            [author] => O'Brien, Tim
                            [title] => MSXML3: A Comprehensive Guide
                            [genre] => Computer
                            [price] => 36.95
                            [publish_date] => 2000-12-01
                            [description] => A former arc
                        )

                    [11] => Array
                        (
                            [-id] => bk112
                            [author] => Galos, Mike
                            [title] => Visual Studio 7: A Comprehensive Guide
                            [genre] => Computer
                            [price] => 49.95
                            [publish_date] => 2001-04-16
                            [description] => A former arc
                        )

                )

        )

)

PHP代码:

<?php 
$con = mysql_connect("localhost","root","") or die('Could not connect: ' . mysql_error());
mysql_select_db("shiv", $con); 
$jsondata = file_get_contents('example.json'); 
$data = json_decode($jsondata, true); echo '<pre>'; 
print_r($data); echo '<pre>'; 
$sql = "insert into book values('','','','','','','','')"; 
?>

1 个答案:

答案 0 :(得分:1)

试试这个:

foreach($data->catalog->book as $book){

     $someVar = $book->author;
     ...
}