重新格式化嵌套数组中的数据

时间:2015-07-03 作者:theflyingant

下面是来自Wordpress Profective Pro插件表单条目数据库表的嵌套数据数组。我想重新格式化它,以便在Wordpress WP\\u list\\u表中使用它,但我无法确定如何更改最内层的嵌套数组。我需要将每一行的格式设置为“product\\u id”=>“4080”,

        Array (
            [30] => Array (
                [user_id] => 2
                [product_id] => 4080
            )
            [31] => Array (
                [user_id] => 5
                [product_id] => 2942
            )
            [32] => Array (
                [user_id] => 4
                [product_id] => 9630
            )
            [33] => Array (
                [user_id] => 3
                [product_id] => 2542
            )
            [34] => Array (
                [user_id] => 7
                [product_id] => 1234
            )
        )
用于生成数组的代码:

        global $wpdb;

        //Retrieve the bids from the database.      
        $form_entries = $wpdb->get_results(\'SELECT * FROM \'. $wpdb->prefix .\'frm_item_metas WHERE field_id in (145,147)\');

        $data = array();

        foreach ( $form_entries as $meta ) {

            if ( ! isset($data[$meta->item_id])){

                $data[$meta->item_id] = array();

            }

            $data[$meta->item_id][] = $meta->meta_value;

        }

        //rename the array keys
        foreach( $data as &$new_values ) {
          $new_values[\'user_id\'] = $new_values[0]; unset( $new_values[0] );
          $new_values[\'product_id\'] = $new_values[1]; unset( $new_values[1] );
        }
        unset($new_values);

    }
我试过摆弄strReplace和introde(),但我真的不知道我在做什么。如果有人能帮忙,我将不胜感激。

所需阵列:

        Array (
            [30] => Array (
                \'user_id\' => \'2\',
                \'product_id\' => \'4080\',
            )
            [31] => Array (
                \'user_id\' => \'5\',
                \'product_id\' => \'2942\',
            )...

1 个回复
SO网友:hampusn

一个快速的解决方案是改变这一点:

    //rename the array keys
    foreach( $data as &$new_values ) {
      $new_values[\'user_id\'] = $new_values[0]; unset( $new_values[0] );
      $new_values[\'product_id\'] = $new_values[1]; unset( $new_values[1] );
    }
对此:

    //rename the array keys
    foreach( $data as &$new_values ) {
      $new_values[\'user_id\'] = (string) $new_values[0]; unset( $new_values[0] );
      $new_values[\'product_id\'] = (string) $new_values[1]; unset( $new_values[1] );
    }
然而,我认为您可以通过只循环一次来缩短工作时间。这是基于您的代码,所以我只是猜测从数据库检索到的数据的结构。

$wpdb->get_results() 具有第二个参数,您可以使用该参数选择检索到的数据的结构。在下面的代码中,我使用关联数组而不是默认对象。这只是我的偏好,在这种情况下并不重要。但如果您不需要通过item_id 不必使用此参数将检索到的对象数据转换为关联数组。

    global $wpdb;

    //Retrieve the form entries from the database.      
    $form_entries = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}frm_item_metas WHERE field_id in (145,147)", ARRAY_A );

    $data = array();
    // Loop through all form entries from query and add them to the data array.
    foreach ( $form_entries as $form_entry ) {
        // Make sure the item_id exist and some kind of meta value too.
        if ( ! empty( $form_entry[ \'item_id\' ] ) && ! empty( $form_entry[ \'meta_value\' ] ) ) {
            $item_id = $form_entry[ \'item_id\' ];
            $entry_value = $form_entry[ \'meta_value\' ];

            // Begin row in data array with the item_id as the numeric key.
            $data[ $item_id ] = array();
            // Add user_id if it exist.
            if ( $entry_value[ \'user_id\' ] ) {
                $data[ $item_id ][ \'user_id\' ] (string) $entry_value[ \'user_id\' ];
            }
            // Add product_id if it exist.
            if ( $entry_value[ \'product_id\' ] ) {
                $data[ $item_id ][ \'product_id\' ] (string) $entry_value[ \'product_id\' ];
            }
        }
    }

    // $data should now hold the structure as you described

    /*
    //rename the array keys
    foreach( $data as &$new_values ) {
      $new_values[\'user_id\'] = $new_values[0]; unset( $new_values[0] );
      $new_values[\'product_id\'] = $new_values[1]; unset( $new_values[1] );
    }
    unset($new_values);
    */
你可能想了解一下protecting queries against SQL injection attacks. 除非这两个field\\u id是静态的。

结束

相关推荐

Wp-login.php只是刷新表单域

我最近将一个客户博客从一个服务器移动到了另一个服务器。从表面上看,一切似乎都很好——博客正在运行,你可以查看没有问题的条目。我们遇到的问题是管理员登录页面。当您输入用户名和密码时,它只会刷新页面。不会显示任何错误消息或提示。我遵循了本页上的提示:http://codex.wordpress.org/Login_TroubleCookies are enabled, tried via several browsers Plugins folder has been renamed to plugi