循环遍历数组/对象的数组

发布于 2024-12-11 09:33:40 字数 1392 浏览 0 评论 0原文

我有类似于以下结构的内容:

Array
(
    [wp_postmeta] => Array
    (
        [0] => stdClass Object
            (
                [meta_id] => 1
                [post_id] => 2
                [meta_key] => _wp_page_template
                [meta_value] => default
            )

    )

    [wp_comments] => Array
    (
        [0] => stdClass Object
            (
                [comment_ID] => 1
                [comment_post_ID] => 1
                [comment_author] => Mr WordPress
                [comment_author_email] => 
                [comment_author_url] => http://wordpress.org/
                [comment_author_IP] => 
                [comment_date] => 2011-10-20 03:06:23
                [comment_date_gmt] => 2011-10-20 03:06:23
                [comment_content] => Hi, this is a comment.
                [comment_karma] => 0
                [comment_approved] => 1
                [comment_agent] => 
                [comment_type] => 
                [comment_parent] => 0
                [user_id] => 0
            )

    )
)

我在这里尝试做的是迭代这些结果,以便我可以使用它们来形成查询。

假设 wp_postmeta 表中的所有数据都从数据库中删除,并且该数组包含该表被删除之前的数据。我想将保存的数据保存在数组中,并使用这些旧值重置表。

即循环遍历数组并将其插入为 sql: INSERT INTO wp_postmeta (meta_id, post_id, meta_key, meta_value) VALUES (1, 2, '_wp_page_template', 'default')

I have something similar to the following structure:

Array
(
    [wp_postmeta] => Array
    (
        [0] => stdClass Object
            (
                [meta_id] => 1
                [post_id] => 2
                [meta_key] => _wp_page_template
                [meta_value] => default
            )

    )

    [wp_comments] => Array
    (
        [0] => stdClass Object
            (
                [comment_ID] => 1
                [comment_post_ID] => 1
                [comment_author] => Mr WordPress
                [comment_author_email] => 
                [comment_author_url] => http://wordpress.org/
                [comment_author_IP] => 
                [comment_date] => 2011-10-20 03:06:23
                [comment_date_gmt] => 2011-10-20 03:06:23
                [comment_content] => Hi, this is a comment.
                [comment_karma] => 0
                [comment_approved] => 1
                [comment_agent] => 
                [comment_type] => 
                [comment_parent] => 0
                [user_id] => 0
            )

    )
)

What I'm trying to do here is iterate over these results so I can use them to form a query.

Assume all the data within the wp_postmeta table is deleted from the database and this array contains the data of that table before it was deleted. I want to take this saved data in the array and reset the table with these old values.

I.e Looping through the array and inserting this as sql: INSERT INTO wp_postmeta (meta_id, post_id, meta_key, meta_value) VALUES (1, 2, '_wp_page_template', 'default')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天涯离梦残月幽梦 2024-12-18 09:33:40
foreach ($outerArray as $tableName => $tableData) { // Loop outer array
  foreach ($tableData as $row) { // Loop table rows
    $cols = $vals = array();
    foreach ($row as $col => $val) { // Loop this row
      $cols[] = $col;
      $vals[] = $val; // You may need to escape this before using it in a query...
    }
    // Build the query
    $query = "INSERT INTO $tableName (".implode(', ',$cols).") VALUES ('".implode("', '",$vals)."')";
    // Do the query here
  }
}

您可以迭代对象属性,就像使用数组一样,并且在 stdClass 中,一切都是公共的,所以上面的内容应该没有问题。

foreach ($outerArray as $tableName => $tableData) { // Loop outer array
  foreach ($tableData as $row) { // Loop table rows
    $cols = $vals = array();
    foreach ($row as $col => $val) { // Loop this row
      $cols[] = $col;
      $vals[] = $val; // You may need to escape this before using it in a query...
    }
    // Build the query
    $query = "INSERT INTO $tableName (".implode(', ',$cols).") VALUES ('".implode("', '",$vals)."')";
    // Do the query here
  }
}

You can iterate over object properties just like you can with an array, and in a stdClass everything is public, so the above should work no problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文