所以我使用 WordPressget_option()來獲取一組用戶的資料庫選項。
所以我有以下方法:
public function deauthorize_instagram_via_button()
{
if (!get_option('instagram_authenticated_users')) {
return;
}
$users = get_option('instagram_authenticated_users');
// If we have single entry, delete the option from the database
if (count($users) === 1) {
delete_option('instagram_authenticated_users');
$this->instagram->delete_cache();
}
// This returns the user_id
var_dump($_POST['user_id']);
die();
// delete_option('instagram_authenticated_users');
exit;
}
哪里get_option('instagram_authenticated_users')回傳:
array(2) {
[0]=>
array(5) {
["username"]=>
string(9) "sem_test1"
["user_id"]=>
int(17841400835712753)
}
[1]=>
array(5) {
["username"]=>
string(12) "sem_test2"
["user_id"]=>
int(17841449642220098)
}
}
然后我有一個$_POST['user_id']which 回傳user_id => 17841449642220098。
如何搜索陣列并洗掉該user_id陣列,然后呼叫update_option('instagram_authenticated_users'),以便在陣列中只保留一個陣列。
因此,在它被洗掉后,get_option('instagram_authenticated_users')呼叫時將如下所示:
array(2) {
[0]=>
array(5) {
["username"]=>
string(9) "sem_test1"
["user_id"]=>
int(17841400835712753)
}
}
謝謝大家!
uj5u.com熱心網友回復:
你可以array_filter像下面這樣使用:
// get current list
$users = get_option('instagram_authenticated_users');
// filter out POST user id
$filtered_users = array_filter($users, function($user){
return $user["user_id"] !== (int) $_POST['user_id'];
});
// save filtered array
update_option('instagram_authenticated_users', $filtered_users);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/435726.html
