我有两个由键和值组成的数组:
由元键组成的数组:
$user_meta_fields = array ( \'first_name\', \'last_name\', \'job_title\', \'college\', \'street_address\',\'address_line2\',\'city\',\'state\',\'country\', \'zip\', \'phone\', \'email\' );
由值组成的数组:
$post_data[\'fname\'] = $_POST[\'fname\'];
$post_data[\'lname\'] = $_POST[\'lname\'];
$post_data[\'job_title\'] = $_POST[\'job_title\'];
$post_data[\'college\'] = $_POST[\'college\'];
$post_data[\'street_address\'] = $_POST[\'street_address\'];
$post_data[\'address_line2\'] = $_POST[\'address_line2\'];
$post_data[\'city\'] = $_POST[\'city\'];
$post_data[\'state\'] = $_POST[\'state\'];
$post_data[\'country\'] = $_POST[\'country\'];
$post_data[\'zip\'] = $_POST[\'zip\'];
$post_data[\'phone\'] = $_POST[\'phone\'];
$post_data[\'email\'] = $_POST[\'email\'];
这是我的循环:
foreach($user_meta_fields as $user_meta_field){
update_user_meta($current_user->ID, $user_meta_field, $post_data);
}
但这对我不起作用。为什么?
SO网友:Domain
确保两个数组中的键相同。键“first\\u name”、“last\\u name”和相应的键“fname”、“lname”是不同的。如果可以使它们相同,那么首先要这样做,否则在使用它们之前需要对这些键进行字符串操作,或者在foreach循环中设置如下条件-
foreach($user_meta_fields as $user_meta_field){
if ($user_meta_field == \'first_name\'){
update_user_meta($current_user->ID, $user_meta_field, $post_data[\'fname\']);
}
else if ($user_meta_field == \'last_name\'){
update_user_meta($current_user->ID, $user_meta_field, $post_data[\'lname\']);
}
else{
update_user_meta($current_user->ID, $user_meta_field, $post_data[$user_meta_field]);
}
}
否则,如果所有键都相同,则通用解决方案-
foreach($user_meta_fields as $user_meta_field){
update_user_meta($current_user->ID, $user_meta_field, $post_data[$user_meta_field]);
}