我有嵌套的數據數組,下面的數據來自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