[英]Store array response as object inside array for options
I would like to been appending objects to instagram
value instead of overwriting them if there is a new object that isn't repeated .如果没有重复的新 object,我想将对象附加到instagram
值而不是覆盖它们。
So I have this piece of code:所以我有这段代码:
if (is_array($api_return)) {
$api_return += ['last_updated' => time()];
$this->instagram->set_user_id($api_return['user_id']);
$this->instagram->set_username($api_return['username']);
$this->instagram->set_access_token($api_return['access_token']);
$this->instagram->set_access_token_expiration($api_return['access_token_expiration']);
$this->instagram->set_last_updated($api_return['last_updated']);
// Show our authenticated message.
$page_content .= $this->get_admin_notice_content('green',
'auth-finished', $this->instagram->get_username());
update_option('instagram', $api_return);
}
What this does, is it takes $api_return
which is a array and groups it into an object and saves it inside the database as this:这是做什么的,它需要$api_return
这是一个数组并将其分组为 object 并将其保存在数据库中,如下所示:
We have option_name
which is instagram
and then the value is:我们有option_name
是instagram
然后值是:
a:5:{s:8:"username";s:16:"saint";s:7:"user_id";i:17841404774727369;s:12:"access_token";s:141:"IGQVJVR1N*****";s:23:"access_token_expiration";i:1650688769;s:12:"last_updated";i:1645536110;}
What I'm attempting to do:我正在尝试做什么:
Now I want to be able to save multiple object where the instagram key doesn't get overwritten but each new api_return gets stored as a new object if it's not the same in an array.现在我希望能够保存多个 object,其中 instagram 密钥不会被覆盖,但如果数组中的每个新 api_return 不相同,则每个新的 api_return 都会存储为新的 object。
Here is an example:这是一个例子:
authenticated_users = [
{s:8:"username";s:16:"saint";s:7:"user_id";i:17841404774727369;s:12:"access_token";s:141:"IGQVJVR1N*****";s:23:"access_token_expiration";i:1650688769;s:12:"last_updated";i:1645536110;}
{s:8:"username";s:16:"test3";s:7:"user_id";i:17841404774727369;s:12:"access_token";s:141:"IGQVJVR1N*****";s:23:"access_token_expiration";i:1650688769;s:12:"last_updated";i:1645536110;}
];
What is the best approach on storing the option value as an array with multiple objects?将选项值存储为包含多个对象的数组的最佳方法是什么?
You have to first load the option an then manually merge the values:您必须先加载选项,然后手动合并值:
$current = get_option("instagram", []);
$newData = [
$api_return,
];
// Merge array $current and $newData into single array so
// $api_return is appended to $current and the option is updated.
update_option("instagram", array_merge($current, $newData));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.