【发布时间】:2020-05-13 08:24:20
【问题描述】:
我正在使用 wordpress 并从 Mysql 中获取一些记录。 我有三种类型的帐户访问权限“Type1”、“Type2”和“Both”。我想将“Both”类型的数组项更改为 Type1 和 Type2 的 2 个数组。
这是我从 Mysql 得到的回复。
Array
(
[0] => stdClass Object
(
[id] => 583
[username] => test1@yopmail.com
[name] => Test 1
[user_pass] => oN3WIffffffMW
[school_name] => Test SCHOOL
[customer_email] => test1@yopmail.com
[trial_account] =>
[expiry_date] => 2021-05-16
[old_expiry_date] =>
[created_date] => 2020-05-13 05:15:24
[status] => 1
[email_status] => 0
[account_access] => Type1
[customer_school_id] => 5455
[is_parent] => 0
[parent_id] => 1534
[logged_count] => 0
[library_count] => 0
[starting_ip_address] =>
[ending_ip_address] =>
[district_id] =>
[company] => test
[37_days_flag] => 0
[stop_email] => 0
[monthly_emails] => 0
[sent_monthly_email] =>
[pwd_for_mail] => hh534446gr
[welcome_email] => 0
[send_bcc] => 0
[is_from_bulk] => 1
[customer_name] => Test 1
[email] => test1@yopmail.com
[id_code] => 55555
[customer_school] => Test SCHOOL
[address] => Test
[city] => Test
[state] => GA
[postal] => 444
[country] => US
[telephone] => 555555
[library_login_url] => 4466dgg
)
[1] => stdClass Object
(
[id] => 584
[username] => test2@yopmail.com
[name] => Test 2
[user_pass] => QJWBsKfgggggszSk
[school_name] => Test SCHOOL
[customer_email] => test2@yopmail.com
[trial_account] =>
[expiry_date] => 2021-05-16
[old_expiry_date] =>
[created_date] => 2020-05-13 05:15:24
[status] => 1
[email_status] => 0
[account_access] => Type2
[customer_school_id] => 3333
[is_parent] => 0
[parent_id] => 1532
[logged_count] => 0
[library_count] => 0
[starting_ip_address] =>
[ending_ip_address] =>
[district_id] =>
[company] => test
[37_days_flag] => 0
[stop_email] => 0
[monthly_emails] => 0
[sent_monthly_email] =>
[pwd_for_mail] => QJWBggg335KzSk
[welcome_email] => 0
[send_bcc] => 0
[is_from_bulk] => 1
[customer_name] => Test 2
[email] => test2@yopmail.com
[id_code] => 566666
[customer_school] => Test SCHOOL
[address] => Test
[city] => Test
[state] => NC
[postal] => 455555
[country] => US
[telephone] => 555555
[library_login_url] => 4433rrfty66
)
[2] => stdClass Object
(
[id] => 585
[username] => test3@yopmail.com
[name] => Test 3
[user_pass] => QJWBsKfgggggszSk
[school_name] => Test SCHOOL
[customer_email] => test3@yopmail.com
[trial_account] =>
[expiry_date] => 2021-05-16
[old_expiry_date] =>
[created_date] => 2020-05-13 05:15:24
[status] => 1
[email_status] => 0
[account_access] => Both
[customer_school_id] => 3333
[is_parent] => 0
[parent_id] => 1532
[logged_count] => 0
[library_count] => 0
[starting_ip_address] =>
[ending_ip_address] =>
[district_id] =>
[company] => test
[37_days_flag] => 0
[stop_email] => 0
[monthly_emails] => 0
[sent_monthly_email] =>
[pwd_for_mail] => QJWBggg335KzSk
[welcome_email] => 0
[send_bcc] => 0
[is_from_bulk] => 1
[customer_name] => Test 2
[email] => test3@yopmail.com
[id_code] => 566666
[customer_school] => Test SCHOOL
[address] => Test
[city] => Test
[state] => NC
[postal] => 455555
[country] => US
[telephone] => 555555
[library_login_url] => 44dd3rrfty66
)
)
这是我的代码:- $all_records 包含上述数组值。
$all_records1 = array();
if (!empty($all_records) && count($all_records) > 0) {
foreach($all_records as $key => $allitems ) {
if($allitems->account_access == 'Both') {
$newItem = $allitems;
$allitems->account_access ='Type1';
$newItem->account_access ='Type2';
array_push($all_records1,$allitems);
array_push($all_records1,$newItem);
} else {
array_push($all_records1,$allitems);
}
}
echo "<pre>"; print_r($all_records1); echo "</pre>"; die;
}
它给了我以下结果。
Array
(
[0] => stdClass Object
(
[id] => 583
[username] => test1@yopmail.com
[name] => Test 1
[user_pass] => oN3WIffffffMW
[school_name] => Test SCHOOL
[customer_email] => test1@yopmail.com
[trial_account] =>
[expiry_date] => 2021-05-16
[old_expiry_date] =>
[created_date] => 2020-05-13 05:15:24
[status] => 1
[email_status] => 0
[account_access] => Type1
[customer_school_id] => 5455
[is_parent] => 0
[parent_id] => 1534
[logged_count] => 0
[library_count] => 0
[starting_ip_address] =>
[ending_ip_address] =>
[district_id] =>
[company] => test
[37_days_flag] => 0
[stop_email] => 0
[monthly_emails] => 0
[sent_monthly_email] =>
[pwd_for_mail] => hh534446gr
[welcome_email] => 0
[send_bcc] => 0
[is_from_bulk] => 1
[customer_name] => Test 1
[email] => test1@yopmail.com
[id_code] => 55555
[customer_school] => Test SCHOOL
[address] => Test
[city] => Test
[state] => GA
[postal] => 444
[country] => US
[telephone] => 555555
[library_login_url] => 4466dgg
)
[1] => stdClass Object
(
[id] => 584
[username] => test2@yopmail.com
[name] => Test 2
[user_pass] => QJWBsKfgggggszSk
[school_name] => Test SCHOOL
[customer_email] => test2@yopmail.com
[trial_account] =>
[expiry_date] => 2021-05-16
[old_expiry_date] =>
[created_date] => 2020-05-13 05:15:24
[status] => 1
[email_status] => 0
[account_access] => Type2
[customer_school_id] => 3333
[is_parent] => 0
[parent_id] => 1532
[logged_count] => 0
[library_count] => 0
[starting_ip_address] =>
[ending_ip_address] =>
[district_id] =>
[company] => test
[37_days_flag] => 0
[stop_email] => 0
[monthly_emails] => 0
[sent_monthly_email] =>
[pwd_for_mail] => QJWBggg335KzSk
[welcome_email] => 0
[send_bcc] => 0
[is_from_bulk] => 1
[customer_name] => Test 2
[email] => test2@yopmail.com
[id_code] => 566666
[customer_school] => Test SCHOOL
[address] => Test
[city] => Test
[state] => NC
[postal] => 455555
[country] => US
[telephone] => 555555
[library_login_url] => 4433rrfty66
)
[2] => stdClass Object
(
[id] => 585
[username] => test3@yopmail.com
[name] => Test 3
[user_pass] => QJWBsKfgggggszSk
[school_name] => Test SCHOOL
[customer_email] => test3@yopmail.com
[trial_account] =>
[expiry_date] => 2021-05-16
[old_expiry_date] =>
[created_date] => 2020-05-13 05:15:24
[status] => 1
[email_status] => 0
[account_access] => Type2
[customer_school_id] => 3333
[is_parent] => 0
[parent_id] => 1532
[logged_count] => 0
[library_count] => 0
[starting_ip_address] =>
[ending_ip_address] =>
[district_id] =>
[company] => test
[37_days_flag] => 0
[stop_email] => 0
[monthly_emails] => 0
[sent_monthly_email] =>
[pwd_for_mail] => QJWBggg335KzSk
[welcome_email] => 0
[send_bcc] => 0
[is_from_bulk] => 1
[customer_name] => Test 2
[email] => test3@yopmail.com
[id_code] => 566666
[customer_school] => Test SCHOOL
[address] => Test
[city] => Test
[state] => NC
[postal] => 455555
[country] => US
[telephone] => 555555
[library_login_url] => 44dd3rrfty66
)
[3] => stdClass Object
(
[id] => 585
[username] => test3@yopmail.com
[name] => Test 3
[user_pass] => QJWBsKfgggggszSk
[school_name] => Test SCHOOL
[customer_email] => test3@yopmail.com
[trial_account] =>
[expiry_date] => 2021-05-16
[old_expiry_date] =>
[created_date] => 2020-05-13 05:15:24
[status] => 1
[email_status] => 0
[account_access] => Type2
[customer_school_id] => 3333
[is_parent] => 0
[parent_id] => 1532
[logged_count] => 0
[library_count] => 0
[starting_ip_address] =>
[ending_ip_address] =>
[district_id] =>
[company] => test
[37_days_flag] => 0
[stop_email] => 0
[monthly_emails] => 0
[sent_monthly_email] =>
[pwd_for_mail] => QJWBggg335KzSk
[welcome_email] => 0
[send_bcc] => 0
[is_from_bulk] => 1
[customer_name] => Test 2
[email] => test3@yopmail.com
[id_code] => 566666
[customer_school] => Test SCHOOL
[address] => Test
[city] => Test
[state] => NC
[postal] => 455555
[country] => US
[telephone] => 555555
[library_login_url] => 44dd3rrfty66
)
)
但它应该给出以下结果
Array
(
[0] => stdClass Object
(
[id] => 583
[username] => test1@yopmail.com
[name] => Test 1
[user_pass] => oN3WIffffffMW
[school_name] => Test SCHOOL
[customer_email] => test1@yopmail.com
[trial_account] =>
[expiry_date] => 2021-05-16
[old_expiry_date] =>
[created_date] => 2020-05-13 05:15:24
[status] => 1
[email_status] => 0
[account_access] => Type1
[customer_school_id] => 5455
[is_parent] => 0
[parent_id] => 1534
[logged_count] => 0
[library_count] => 0
[starting_ip_address] =>
[ending_ip_address] =>
[district_id] =>
[company] => test
[37_days_flag] => 0
[stop_email] => 0
[monthly_emails] => 0
[sent_monthly_email] =>
[pwd_for_mail] => hh534446gr
[welcome_email] => 0
[send_bcc] => 0
[is_from_bulk] => 1
[customer_name] => Test 1
[email] => test1@yopmail.com
[id_code] => 55555
[customer_school] => Test SCHOOL
[address] => Test
[city] => Test
[state] => GA
[postal] => 444
[country] => US
[telephone] => 555555
[library_login_url] => 4466dgg
)
[1] => stdClass Object
(
[id] => 584
[username] => test2@yopmail.com
[name] => Test 2
[user_pass] => QJWBsKfgggggszSk
[school_name] => Test SCHOOL
[customer_email] => test2@yopmail.com
[trial_account] =>
[expiry_date] => 2021-05-16
[old_expiry_date] =>
[created_date] => 2020-05-13 05:15:24
[status] => 1
[email_status] => 0
[account_access] => Type2
[customer_school_id] => 3333
[is_parent] => 0
[parent_id] => 1532
[logged_count] => 0
[library_count] => 0
[starting_ip_address] =>
[ending_ip_address] =>
[district_id] =>
[company] => test
[37_days_flag] => 0
[stop_email] => 0
[monthly_emails] => 0
[sent_monthly_email] =>
[pwd_for_mail] => QJWBggg335KzSk
[welcome_email] => 0
[send_bcc] => 0
[is_from_bulk] => 1
[customer_name] => Test 2
[email] => test2@yopmail.com
[id_code] => 566666
[customer_school] => Test SCHOOL
[address] => Test
[city] => Test
[state] => NC
[postal] => 455555
[country] => US
[telephone] => 555555
[library_login_url] => 4433rrfty66
)
[2] => stdClass Object
(
[id] => 585
[username] => test3@yopmail.com
[name] => Test 3
[user_pass] => QJWBsKfgggggszSk
[school_name] => Test SCHOOL
[customer_email] => test3@yopmail.com
[trial_account] =>
[expiry_date] => 2021-05-16
[old_expiry_date] =>
[created_date] => 2020-05-13 05:15:24
[status] => 1
[email_status] => 0
[account_access] => Type1
[customer_school_id] => 3333
[is_parent] => 0
[parent_id] => 1532
[logged_count] => 0
[library_count] => 0
[starting_ip_address] =>
[ending_ip_address] =>
[district_id] =>
[company] => test
[37_days_flag] => 0
[stop_email] => 0
[monthly_emails] => 0
[sent_monthly_email] =>
[pwd_for_mail] => QJWBggg335KzSk
[welcome_email] => 0
[send_bcc] => 0
[is_from_bulk] => 1
[customer_name] => Test 2
[email] => test3@yopmail.com
[id_code] => 566666
[customer_school] => Test SCHOOL
[address] => Test
[city] => Test
[state] => NC
[postal] => 455555
[country] => US
[telephone] => 555555
[library_login_url] => 44dd3rrfty66
)
[3] => stdClass Object
(
[id] => 585
[username] => test3@yopmail.com
[name] => Test 3
[user_pass] => QJWBsKfgggggszSk
[school_name] => Test SCHOOL
[customer_email] => test3@yopmail.com
[trial_account] =>
[expiry_date] => 2021-05-16
[old_expiry_date] =>
[created_date] => 2020-05-13 05:15:24
[status] => 1
[email_status] => 0
[account_access] => Type2
[customer_school_id] => 3333
[is_parent] => 0
[parent_id] => 1532
[logged_count] => 0
[library_count] => 0
[starting_ip_address] =>
[ending_ip_address] =>
[district_id] =>
[company] => test
[37_days_flag] => 0
[stop_email] => 0
[monthly_emails] => 0
[sent_monthly_email] =>
[pwd_for_mail] => QJWBggg335KzSk
[welcome_email] => 0
[send_bcc] => 0
[is_from_bulk] => 1
[customer_name] => Test 2
[email] => test3@yopmail.com
[id_code] => 566666
[customer_school] => Test SCHOOL
[address] => Test
[city] => Test
[state] => NC
[postal] => 455555
[country] => US
[telephone] => 555555
[library_login_url] => 44dd3rrfty66
)
)
知道我在这段代码中缺少什么吗?
【问题讨论】:
-
下定决心!你真正使用的是什么代码?
-
新代码会产生同样的问题吗?
-
我认为在那种情况下你需要
print_r($all_records1, true); -
@RiggsFolly ,对不起,我之前输入了错误的代码,现在是正确的代码
-
感谢 Arun 与我们分享这个出色的数组示例。非常感谢。
标签: php arrays wordpress loops