我有嵌套的数据数组,下面的数据来自Wordpress Formidable Pro插件表单条目数据库表。我想重新格式化它,所以我可以在Wordpress WP_list_table中使用它,但我无法弄清楚如何改变最内层的嵌套数组。我需要每一行进行格式化像'product_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乱搞和内爆(),但我真的不知道是什么我正在做。如果有人能帮忙,我会很感激。
编辑: 期望的数组:
Array (
[30] => Array (
'user_id' => '2',
'product_id' => '4080',
)
[31] => Array (
'user_id' => '5',
'product_id' => '2942',
)...
它不清楚你想要什么,你可以用你想要的数组编辑你的问题吗? – Mihai