改变嵌套数组中的数据
Alter data within a nested array
我有下面的嵌套数据数组,这些数据来自 Wordpress Formidable Pro 插件表单条目数据库 table。我想重新格式化它,以便我可以在 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 和 implode() 但我真的不知道我在做什么。如果有人能提供帮助,我将不胜感激。
编辑:
所需数组:
Array (
[30] => Array (
'user_id' => '2',
'product_id' => '4080',
)
[31] => Array (
'user_id' => '5',
'product_id' => '2942',
)...
数组中没有方括号,这正是 print_r 标记数组中键的方式。据我所知,数组是你想要的格式。
这是为了更好地理解问题...我创建了一个数组。
$test = array(
30 => array( 'user_id' => '2', 'item_id' => '4080' ),
31 => array( 'user_id' => '5', 'item_id' => '2942' ),
);
这就是 print_r($test)
让它看起来像:
Array
(
[30] => Array
(
[user_id] => 2
[item_id] => 4080
)
[31] => Array
(
[user_id] => 5
[item_id] => 2942
)
)
这就是 var_export($test) 让它看起来像:
array (
30 =>
array (
'user_id' => '2',
'item_id' => '4080',
),
31 =>
array (
'user_id' => '5',
'item_id' => '2942',
),
)
这就是 var_dump($test) 让它看起来像:
array(2) {
[30]=>
array(2) {
["user_id"]=>
string(1) "2"
["item_id"]=>
string(4) "4080"
}
[31]=>
array(2) {
["user_id"]=>
string(1) "5"
["item_id"]=>
string(4) "2942"
}
}
数组是一种为键赋值的数据结构;它不是您在 运行 通过显示该结构表示形式的函数时看到的字符串。
我有下面的嵌套数据数组,这些数据来自 Wordpress Formidable Pro 插件表单条目数据库 table。我想重新格式化它,以便我可以在 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 和 implode() 但我真的不知道我在做什么。如果有人能提供帮助,我将不胜感激。
编辑: 所需数组:
Array (
[30] => Array (
'user_id' => '2',
'product_id' => '4080',
)
[31] => Array (
'user_id' => '5',
'product_id' => '2942',
)...
数组中没有方括号,这正是 print_r 标记数组中键的方式。据我所知,数组是你想要的格式。
这是为了更好地理解问题...我创建了一个数组。
$test = array(
30 => array( 'user_id' => '2', 'item_id' => '4080' ),
31 => array( 'user_id' => '5', 'item_id' => '2942' ),
);
这就是 print_r($test)
让它看起来像:
Array
(
[30] => Array
(
[user_id] => 2
[item_id] => 4080
)
[31] => Array
(
[user_id] => 5
[item_id] => 2942
)
)
这就是 var_export($test) 让它看起来像:
array (
30 =>
array (
'user_id' => '2',
'item_id' => '4080',
),
31 =>
array (
'user_id' => '5',
'item_id' => '2942',
),
)
这就是 var_dump($test) 让它看起来像:
array(2) {
[30]=>
array(2) {
["user_id"]=>
string(1) "2"
["item_id"]=>
string(4) "4080"
}
[31]=>
array(2) {
["user_id"]=>
string(1) "5"
["item_id"]=>
string(4) "2942"
}
}
数组是一种为键赋值的数据结构;它不是您在 运行 通过显示该结构表示形式的函数时看到的字符串。