【问题标题】:How to save special array data into Three MySQL table in PHP如何在 PHP 中将特殊数组数据保存到三个 MySQL 表中
【发布时间】:2016-09-29 07:46:25
【问题描述】:

我有需要插入 MySQL 表的 JSON。

我已经做的是,使用以下方法将其转换为数组:

$json_data = json_decode($geo_json, true);

并得到数组的输出,但我不知道如何插入第二个和第三个 MySQL 表。

我的 MySQL 表:

表一:

geo_id | user_id | geo_title | geo_description | geo_date |geo_status |geo_action |action_reason | geo_json | remote_ip | map_type |geo_snapshot

我可以轻松插入上表,但问题在下面列出的表二和表三中。

表2:

id | layer_id | map_id | layer_name | user_id | draw_type | latlng | radious 

表3:

data_id | geo_key | geo_value | map_id | layer_id 

我得到的数组:

Array
(
    [type] => FeatureCollection
    [features] => Array
        (
            [0] => Array
                (
                    [type] => Feature
                    [properties] => Array
                        (
                            [action] => a
                            [poi_name] => a
                            [fac_type] => 17
                            [ph_number] => a
                            [hno] => a
                            [postcode] => a
                            [mail] => a
                            [str_nm] => a
                            [photo] => developer-page.png
                            [comment] => a
                            [url] => a
                            [sub_loc] => a
                            [employee] => a
                        )

                    [geometry] => Array
                        (
                            [type] => Point
                            [coordinates] => Array
                                (
                                    [0] => 88.434448242188
                                    [1] => 22.510971144638
                                )

                        )

                )

            [1] => Array
                (
                    [type] => Feature
                    [properties] => Array
                        (
                            [action] => b
                            [poi_name] => b
                            [fac_type] => 18
                            [ph_number] => b
                            [hno] => b
                            [postcode] => b
                            [mail] => b
                            [str_nm] => b
                            [photo] => 1475131600_developer-page.png
                            [comment] => b
                            [url] => b
                            [sub_loc] => b
                            [employee] => b
                        )

                    [geometry] => Array
                        (
                            [type] => Point
                            [coordinates] => Array
                                (
                                    [0] => 88.321151733398
                                    [1] => 22.50906814933
                                )

                        )

                )

        )

) 

现在的问题是将以上数据插入两个单独的表中:

表2:这只需要从上面的php数组中插入draw_type | latlng

示例:draw_ type: point and latlng : coordinates

表 3: 这需要从 PHP 数组上方插入 geo_key | geo_value | map_id | layer_id。 示例:

geo_key : properties [action,poi_name,fac_type,ph_number,hno,postcode,mail,str_nm, photo, comment, url, sub_loc, employee]

geo_value : [properties values ] 

map_id :[this will be table 1 insert id] 

layer_id : [this can be blank] 

请指导我并告诉我如何开始。

【问题讨论】:

  • 您是问如何通过提供特定的索引值从数组中提取数据?
  • 你知道如何使用foreach 语句吗?
  • @HZS 我想知道如何将上述数组数据插入到 MySQL 三个不同的表中作为提及字段。您可以看到三个不同的表,采用不同的方法。第三个表只接受键:值数据。
  • @RiggsFolly 我知道 foreach 声明。请在此处使用示例向我展示 foreach 语句如何提供帮助。
  • 这不是 SO 的工作方式。您向我们展示您的尝试,告诉我们您在哪里遇到问题,我们会帮助解决该问题。 我们不按照规范工作所以不是免费的编码服务

标签: php arrays json mysqli


【解决方案1】:

$json_data["features"][$array_index]["geometry"]["type"]

对于表 2:

foreach($json_data["features"] as $info){
    $type = $info["geometry"]["type"];
    $latlng_0 = $info["geometry"]["coordinates"][0];
    $latlng_1 = $info["geometry"]["coordinates"][1];

    // DO INSRET with $type, $latling_0, $latling_1
    $sql = "INSERT INTO Table2 (draw_type, latlng0, latlng1) VALUES ('".$type."','".$latlng_0."','".$latlng_1."')";
    ....
}

对于表 3:

'map_id' 是 table1 中的自增键吗?

您需要先通过 select * where (conditions) 知道 map_id 成功向table1插入数据后

如果 'layer_id' 可以接受空白(null)数据,那么如果您在 INSERT 命令中不指定值就可以了。只需确保您的表格设置正确即可。

【讨论】:

  • 谢谢@HZS,你真的很棒......我让它工作并且能够轻松获取所有数据:foreach($json_data["features"] as $info){ echo $type = $info["geometry"]["type"].'<br>'; echo $latlng_0 = $info["geometry"]["coordinates"][0].'<br>'; echo $latlng_1 = $info["geometry"]["coordinates"][1].'<br>'; foreach($info["properties"] as $mydata => $value){ echo $action = $mydata.'<br>'; } }
猜你喜欢
  • 1970-01-01
  • 2017-07-28
  • 2012-09-29
  • 2016-09-06
  • 1970-01-01
  • 1970-01-01
  • 2018-02-17
  • 2010-12-31
  • 2017-02-03
相关资源
最近更新 更多