dongtong5242 2014-08-21 20:53
浏览 47
已采纳

PHP:MySQL数据库的数组

I'd like to put some variables of my array into a MySQL Database.

My array is called $data and looks like this :

Array
(
[0] => Array
    (
        [created_at] => Wed Aug 20 19:38:58 +0000 2014
        [location] => Hollywood, CA
    )

[1] => Array
    (
        [created_at] => Wed Aug 20 16:45:48 +0000 2014
        [location] => Ventura County
    )

[2] => Array
    (
        [created_at] => Wed Aug 20 01:39:03 +0000 2014
        [location] => Berkeley, CA
    )

[3] => Array
    (
        [created_at] => Tue Aug 19 23:53:54 +0000 2014
        [location] => Charlotte, NC
    )

)

Then I created the MySQL "maj" with that query :

CREATE TABLE maj (id SERIAL, created_at TEXT, location TEXT)

I know how to create a variable $location and put it into the database (something like
$location= $data['location']). But I see the thing when the array is only composed of one location and not three..

So how could I do to populate my database with the three locations and the three created_at ?

Thank you !


Ok, I've edited my question by adding Aleatoric's snippet:

$get_tweets = $twitter_oauth->get (URL)
$tweets = json_encode($get_tweets);
$my_arr = json_decode($tweets, true);
$data = array();

foreach($my_arr["statuses"] as $status) {
    $data[] = array(
      "created_at" => $status["created_at"],
     "location" => $status["user"]["location"]
    );
}

$rows = [
    [
        'created_at'=>'date1',
        'location'=>'location1'
    ],[
        'created_at'=>'date2',
        'location'=>'location2'
    ]
];

foreach ($rows as $row){
    $date = $row['created_at'];
    $location = $row['location'];
    //run SQL insert
}
  • 写回答

2条回答 默认 最新

  • douweilaton2762 2014-08-21 21:14
    关注

    If you want to do something for each element in an array, you need a foreach loop, e.g.

    $rows = [
        [
            'created_at'=>'date1',
            'location'=>'location1'
        ],[
            'created_at'=>'date2',
            'location'=>'location2'
        ]
    ];
    
    foreach ($rows as $row){
        $date = $row['created_at'];
        $location = $row['location'];
        //run SQL insert
    }
    

    EDIT based on the updated code sample:

    $get_tweets = $twitter_oauth->get (URL)
    $tweets = json_encode($get_tweets);
    $my_arr = json_decode($tweets, true);
    
    //Declare the multidimensional data array which will contain the rows
    $data = array();
    
    //Populate the data array with arrays of values
    foreach($my_arr["statuses"] as $status) {
    $data[] = array(
        "created_at" => $status["created_at"],
       "location" => $status["user"]["location"]
      );
    }
    
    //Iterate over the data array
    foreach ($data as $row){
        $date = $row['created_at'];
        $location = $row['location'];
        //run SQL insert
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备