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

如何将此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 能给我一些人生建议吗
  • ¥15 mac电脑,安装charles后无法正常抓包
  • ¥18 visio打开文件一直显示文件未找到
  • ¥15 请教一下,openwrt如何让同一usb储存设备拔插后设备符号不变?
  • ¥30 使用quartz框架进行分布式任务定时调度,启动了两个实例,但是只有一个实例参与调度,另外一个实例没有参与调度,不知道是为什么?请各位帮助看一下原因!!
  • ¥50 怎么获取Ace Editor中的python代码后怎么调用Skulpt执行代码
  • ¥30 fpga基于dds生成幅值相位频率和波形可调的容易信号发生器。
  • ¥15 R语言shiny包和ncdf4包报错
  • ¥15 origin绘制有显著差异的柱状图和聚类热图
  • ¥20 simulink实现滑模控制和pid控制对比,提现前者优势