Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在数组/对象数组中循环_Php_Arrays_Wordpress_Object_Loops - Fatal编程技术网

Php 在数组/对象数组中循环

Php 在数组/对象数组中循环,php,arrays,wordpress,object,loops,Php,Arrays,Wordpress,Object,Loops,我有类似于以下结构的东西: Array ( [wp_postmeta] => Array ( [0] => stdClass Object ( [meta_id] => 1 [post_id] => 2 [meta_key] => _wp_page_template [meta_val

我有类似于以下结构的东西:

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_Posteta表中的所有数据都已从数据库中删除,并且此数组包含删除前该表的数据。我想在数组中获取保存的数据,并用这些旧值重置表

也就是说,在数组中循环并将其作为sql插入:插入wp_postETA(meta_id、post_id、meta_key、meta_value)值(1,2’,_wp_page_模板’,“默认值”)


您可以像使用数组一样,在
stdClass
中,所有内容都是公共的,因此上面的操作应该不会有问题。

问题是什么?问题是我不知道如何迭代这些结果以形成查询。查看WP API如何将对象作为注释插入。然后,您只需要对数组进行foreach并按值触发insert。对象作为注释吗?我能找到的唯一函数是$wpdb->insert($wpdb->table_name,array('field_one'=>$newvalueone,'field_two'=>$newvaluetwo));戴夫,非常感谢-我很快会试试的。但有一个问题-假设我们以field=>value:[comment\u content]=>现在是聚会时间的sss为例。这无疑会打破你的疑问-纠正这个问题的简单方法?哦,刚才看到你在$val旁边的评论。对了:P
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
  }
}