php - 使用 PHP 在 MySQL 中嵌套 JSON 存储
问题描述
我想就以下问题寻求您的帮助。
我得到了类似于下面在 PHP 中提到的嵌套 json。我想将数据存储在 MySQL db 中,但我遇到了变量信息的问题。
我的问题是数据不一致。正如您在下面的 json 中看到的:
[6] => Array
(
[pflds] => Array
(
[1] => Array
(
[id] => 1
[n] => registration_plate
[v] => xxxxxx
)
[2] => Array
(
[id] => 2
[n] => vehicle_type
[v] => xxxxx
)
[3] => Array
(
[id] => 3
[n] => brand
[v] => xxxx
)
)
[pfldsmax] => 0
)
[7] => Array
(
[pflds] => Array
(
[1] => Array
(
[id] => 1
[n] => vehicle_type
[v] => Трактор
)
[2] => Array
(
[id] => 2
[n] => registration_plate
[v] => xxxxx
)
[3] => Array
(
[id] => 3
[n] => brand
[v] => John Deere
)
)
[pfldsmax] => 0
)
一旦登记牌
数组 -> [6] -> [1] -> [n] 牌照
接着
数组 -> [7] -> [2] -> [n] 牌照
所以我不能轻易地说“插入”,因为列的位置不同。字段 [n] 在任何时候都具有相同的名称,但 [id] 可能会有所不同。
我想不出任何使用条件。但我想关注字段 [n] ,它将说明值将存储在哪一列中。
有什么建议或任何其他方法吗?
我是这个领域的新手。感谢您的帮助。
解决方案
您可以foreach
多次使用循环,例如:
foreach($ar as $ind1=>$set) { // ind1 = 6 or 7
foreach($set as $key=>$subar){ // key = 'pflds' or 'pfldsmax'
if (is_array($subar)){ // if subar is an array
foreach($subar as $item){
if ($item['n'] === 'registration_plate'){
// code for insert into DB
echo $item['id'].PHP_EOL;
}
}
}
}
}
推荐阅读
- python - Python Plotly Sankey Graph 未显示
- angular6 - ngx-daterangepicker 的 Angular6 问题
- javascript - 如何在客户端Angularjs上压缩图像大小?
- google-play - 股票市场的技术图表分析
- html - 引导程序中的 PRE 标记不显示所有代码
- git - 代码更改时提交/推送的通知
- laravel-5 - 如何为 laravel 刀片定义提示路径
- apache-spark - 使用 spark-xml 从 pyspark 数据框中选择嵌套列
- javascript - 在全日历中显示加号图标
- java - 学习 Java - 如何根据类型选择不同的服务 bean?