Php 如何将json多维数组数据插入数据库
PHP代码:Php 如何将json多维数组数据插入数据库,php,Php,PHP代码: Array ( [catalog] => Array ( [book] => Array ( [0] => Array ( [-id] => bk101 [author] =>
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
)
)
)
)
试试这个:
在问题中添加您的代码您想如何插入它?作为一个表字段中的json对象?!请向我们展示您的代码到目前为止实际尝试了什么以及您的问题是什么。否则,一般的“如何”问题更适合[您选择的搜索引擎]而不是Stackoverflow。@ChetanAmeta先生,此页上方有一个数组,我想使用php将该数据插入数据库。我是php新手。我不知道如何从3d数组中获取所有数据。@Mohammad先生,我只是想插入该数据(数组数据)使用php进入数据库。我是php新手。我不知道如何从3d数组中获取所有数据。实际上,在书之后还有一个数组。。
<?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('','','','','','','','')";
?>
foreach($data->catalog->book as $book){
$someVar = $book->author;
...
}