У меня есть мета-поле пользователя с именем simplefavorites
, в котором хранятся все избранные сообщения для пользователей. Используемый плагин: http://favoriteposts.com/
Этот код $all_meta_for_user = get_user_meta( get_current_user_id(), 'simplefavorites' );
извлекает поле:
Array
(
[0] => Array
(
[0] => Array
(
[site_id] => 1
[posts] => Array
(
[1] => 9004111222003228
[2] => 9004111222031615
)
[groups] => Array
(
[0] => Array
(
[group_id] => 1
[site_id] => 1
[group_name] => Default List
[posts] => Array
(
[1] => 9004111222003228
[2] => 9004111222031615
)
)
)
)
)
)
Из дампа данных мне пришлось удалить сообщения с идентификатором "9004111222003228 ", поэтому я написал функцию для ее обновления.
function removeElement(array $list, string $search): array {
foreach($list as $key => $value) {
if (is_array($value)) {
$list[$key] = removeElement($value, $search);
} else if ($value == $search) {
unset($list[$key]);
}
}
return $list;
}
И использовал это, чтобы получить обновленное поле:
removeElement($all_meta_for_user, "9004111222003228");
Это дало мне:
Array
(
[0] => Array
(
[0] => Array
(
[site_id] => 1
[posts] => Array
(
[2] => 9004111222031615
)
[groups] => Array
(
[0] => Array
(
[group_id] => 1
[site_id] => 1
[group_name] => Default List
[posts] => Array
(
[2] => 9004111222031615
)
)
)
)
)
)
Теперь, когда я использую update_user_meta( get_current_user_id(), 'simplefavorites', removeElement($all_meta_for_user, "9004111222003228") );
, я получаю следующую ошибку:
Notice: Undefined index: site_id in /var/www/html/wp-content/plugins/favorites/app/Entities/User/UserRepository.php on line 122
Notice: Undefined index: posts in /var/www/html/wp-content/plugins/favorites/app/Entities/User/UserRepository.php on line 124
Notice: Undefined index: site_id in /var/www/html/wp-content/plugins/favorites/app/Helpers.php on line 96
Notice: Undefined variable: ids in /var/www/html/wp-content/toolset-customizations/get_favorite_elements.php on line 30
Notice: Undefined index: site_id in /var/www/html/wp-content/plugins/favorites/app/Entities/User/UserRepository.php on line 122
Notice: Undefined index: posts in /var/www/html/wp-content/plugins/favorites/app/Entities/User/UserRepository.php on line 124
Notice: Undefined index: site_id in /var/www/html/wp-content/plugins/favorites/app/Helpers.php on line 96
Notice: Undefined variable: ids in /var/www/html/wp-content/toolset-customizations/get_favorite_elements.php on line 30
Notice: Undefined index: site_id in /var/www/html/wp-content/plugins/favorites/app/Entities/User/UserRepository.php on line 122
Notice: Undefined index: posts in /var/www/html/wp-content/plugins/favorites/app/Entities/User/UserRepository.php on line 124
Notice: Undefined index: site_id in /var/www/html/wp-content/plugins/favorites/app/Helpers.php on line 96
Notice: Undefined variable: ids in /var/www/html/wp-content/toolset-customizations/get_favorite_elements.php on line 30
Я думаю, что способ вставки многомерного массива неправильный. Должен ли я сериализовать, а затем обновить?