duandaotuo5542 2014-02-04 09:52
浏览 1344
已采纳

如何将此json数组保存到MYSQL数据库

I want to save this json array that my fb-login returns to a database. However I don't really know how to convert that into a string using php.

[{"id":"108124505876479","name":"Wakeboarding"},{"id":"112003352149145","name":"Bouldering"},{"id":"110008522357035","name":"Handball"}]

I have tried this code below but I am even not quite sure anymore whether what the fb login return is a json array or not.

$json = '{"id":"108124505876479","name":"Wakeboarding"},{"id":"112003352149145","name":"Bouldering"},{"id":"110008522357035","name":"Handball"}';

var_dump(json_decode($json));

How would I be able to save the different sports to my database in a string?

Any help would be much appreciated.

Thanks.

EDIT: The upper part returns NULL.


SECOND EDIT:

if (array_key_exists('sports', $me)){


                    $json = '$me['sports']'; PROBLEM IS HERE
                    $data = json_decode($json, true);
                    $sports = array();
                    foreach ($data as $item) {
                        $sports[] = $item['name'];
                    }

                    $user->fb_sports = $sports;
                }
  • 写回答

5条回答 默认 最新

  • dongxin8709 2014-02-04 10:06
    关注
    $json = '[{"id":"108124505876479","name":"Wakeboarding"},{"id":"112003352149145","name":"Bouldering"},{"id":"110008522357035","name":"Handball"}]';
    $data = json_decode($json, true);
    $sports = array();
    foreach ($data as $item) {
        $sports[] = $item['name'];
    }
    

    if you want commas separating them:

    $output = implode(', ', $sports);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示