简体   繁体   English

如何将嵌套/多维数组中的值存储在 MySQL 数据库中?

[英]How can I store values from a nested/multidimensional array in a MySQL database?

My array looks like this:我的数组如下所示:

Array  
(  
[0] => Array
    (
        [created_at] => Sat Jun 25 21:22:20 +0000 2011
        [text] => i'm eating apple
        [sender] => Array
            (
                [name] => mark o
                [created_at] => Wed May 28 18:21:03 +0000 2008
            )
        [recipient] => Array
            (
                [created_at] => Mon Jun 21 19:48:50 +0000 2010
                [screen_name] => playassassin
            )
        [sender_screen_name] => mark
        [recipient_screen_name] => james
    )

[1] => Array
    (
        [created_at] => Mon Jun 20 10:52:37 +0000 2011
        [text] => My bday in 5 minutes
        [sender] => Array
            (
                [name] => mark o
                [created_at] => Wed May 28 18:21:03 +0000 2008
            )
        [recipient] => Array
            (
                [created_at] => Mon Jun 21 19:48:50 +0000 2010
                [screen_name] => james
            )
        [sender_screen_name] => mark
        [recipient_screen_name] => james
    )  
)  

This is a simplified version of a direct message feed from the Twitter API.这是来自 Twitter API 的直接消息提要的简化版本。 I will be requesting the latest DMs every X minutes using PHP and the twitter-async library, and then I want to store parts of the array in a database table.我将使用 PHP 和 twitter-async 库每 X 分钟请求一次最新的 DM,然后我想将部分数组存储在数据库表中。 Each DM should be stored in its own row.每个 DM 应存储在其自己的行中。
The table the data will be stored in:数据将存储在的表中:

CREATE TABLE `dms` (
    `postid` INT(12) NOT NULL,
    `text` VARCHAR(140) NOT NULL COLLATE 'utf8_unicode_ci',
    `sender` VARCHAR(20) NOT NULL COLLATE 'utf8_unicode_ci',
    `sender_id` VARCHAR(20) NOT NULL COLLATE 'utf8_unicode_ci',
    `date_created` DATETIME NOT NULL
)

I really don't know how to go about doing this and would greatly appreciate some help.我真的不知道如何 go 这样做,非常感谢一些帮助。

If it simplifies things at all, I only need to pull values from the second layer in. Also, I don't know how many rows I'll be adding.如果它完全简化了事情,我只需要从第二层中提取值。另外,我不知道我将添加多少行。


I've been messing around, and this code almost does what I need:我一直在搞乱,这段代码几乎可以满足我的需要:

foreach ( $adms as $dm ) {

    foreach ( $dm as $key => $value ) {
        $q = "INSERT INTO dms SET text = '{$value}'";
        mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
  }

}

Of course, that just stores each second level value in a new row in the "text" column.当然,这只是将每个二级值存储在“文本”列的新行中。 If you can help me complete it to do what I've asked, I'll mark your answer.如果您可以帮助我完成它以完成我的要求,我会标记您的答案。

Well...you are only entering one value.嗯...您只输入一个值。

Try something like this: -尝试这样的事情: -

foreach ( $adms as $dm ) {
    $q = "INSERT INTO dms(text, sender, sender_id, date_created) values('".$dm["text"]."', '".$dm["sender_screen_name"]."', '".$dm["sender"]["name"]."', now())";
    mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
}

Define a parent_postid, so insert the parent, get it's id and insert sub records with that id in the parent_postid定义一个parent_postid,所以插入父,获取它的id,并在parent_postid中插入具有该id的子记录

CREATE TABLE `dms` (
    `postid` INT(12) NOT NULL,
    `parent_postid` INT(12) NOT NULL,
    `text` VARCHAR(140) NOT NULL COLLATE 'utf8_unicode_ci',
    `sender` VARCHAR(20) NOT NULL COLLATE 'utf8_unicode_ci',
    `sender_id` VARCHAR(20) NOT NULL COLLATE 'utf8_unicode_ci',
    `date_created` DATETIME NOT NULL
)

this can be done to many n-levels... and each sub can be a parent and so on...这可以在许多 n 级上完成……每个子都可以是父级,依此类推……

What you want to achieve is also called shredding, eg xml shredding.您想要实现的也称为切碎,例如 xml 切碎。 The basic idea is to imagine your array as a tree.基本思想是将您的数组想象成一棵树。 Once you have the information as a tree, you can store every node in a linear table.一旦您将信息作为树,您可以将每个节点存储在线性表中。 Along with every node (ie contained info) you store its parent/child relationship and whatever you need to restore your initial structure.与每个节点(即包含的信息)一起,您存储其父/子关系以及恢复初始结构所需的任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM