[英]How can I write / read & delete nested arrays with WordPress (PHP)
I've a bit of a problem realizing my plan.我在实现我的计划时遇到了一些问题。 My initial situation is the following:
我的初始情况如下:
I have an array with different contents.我有一个包含不同内容的数组。 In this example it looks like this:
在这个例子中,它看起来像这样:
$tmp[ get_current_user_id() ] = array(
'id' => 5,
'ref' => true
);
Now I want to save this array under a certain key using the function add_post_meta()
in WordPress, which works so far:现在我想使用 WordPress 中的
add_post_meta()
函数将这个数组保存在某个键下,到目前为止它有效:
add_post_meta( $post_id, 'tmp', $tmp );
As you can see above, I took the current User-ID for the key of the array.正如你在上面看到的,我把当前的用户 ID 作为数组的键。 If I now have 2 entries in my array and output it, I have the following scheme:
如果我的数组中现在有 2 个条目并输出它,我有以下方案:
$tmp = get_post_meta( $post_id, 'tmp' );
error_log( print_r( $tmp, true ) );
----------------------------------------
Array
(
[0] => Array
(
[32] => Array
(
[id] => 10
[ref] =>
)
)
[1] => Array
(
[44] => Array
(
[id] => 20
[ref] =>
)
)
)
At this point, however, I am not getting any further.然而,在这一点上,我没有进一步。 For me, there are now two questions:
对我来说,现在有两个问题:
get_current_user_id()
(as my key) function without running through multiple loops?get_current_user_id()
(作为我的键)函数访问正确的数组? Result after deletion:删除后的结果:
Array
(
[0] => Array
(
[32] => Array
(
[id] => 10
[ref] =>
)
)
)
There is a function delete_post_meta()
, but I don't know how it should work with nested arrays.有一个函数
delete_post_meta()
,但我不知道它应该如何处理嵌套数组。 I am very grateful for any help!我非常感谢任何帮助!
add_post_meta
( https://developer.wordpress.org/reference/functions/add_post_meta/ ) has a $unique
flag defaulting to false. add_post_meta
( https://developer.wordpress.org/reference/functions/add_post_meta/ ) 有一个$unique
标志,默认为 false。 That is what causes the outer array - each time you add a new single element array with an id and ref, it is just appended to a list of elements under tmp
.这就是导致外部数组的原因 - 每次添加带有 id 和 ref 的新单个元素数组时,它只是附加到
tmp
下的元素列表。
However, if you enable $unique
, you will overwrite the existing array each time - which I assume is not what you want, either.但是,如果您启用
$unique
,您每次都会覆盖现有数组 - 我认为这也不是您想要的。 Therefore, instead of using add_post_meta
, you should do the following:因此,您应该执行以下操作,而不是使用
add_post_meta
:
tmp
post meta and store it in a variabletmp
post meta 并将其存储在变量中update_post_meta
to overwrite tmp
with the modified arrayupdate_post_meta
用修改后的数组覆盖tmp
For get_post_meta
, make sure to set $single
to true (3rd argument) - otherwise you will again get it wrapped in an outer array):对于
get_post_meta
,请确保将$single
设置$single
true(第三个参数)-否则您将再次将其包装在外部数组中):
// step 1: retrieve current value of 'tmp' meta
$user_list = get_post_meta($post_id, 'tmp', true);
if ($user_list === false) { // meta wasn't set yet
$user_list = []; // initialize array
}
// step 2: set information for current user
$user_list[ get_current_user_id() ] = [ 'id' => 1337, 'ref' => '...'];
// step 3: overwrite 'tmp' post meta with updated array
update_post_meta($post_id, 'tmp', $user_list);
To then delete an element, do the following:要删除元素,请执行以下操作:
$user_list = get_post_meta($post_id, 'tmp', true);
if ($user_list === false) { // meta wasn't set yet
$user_list = []; // initialize array
}
// step 2: delete desired user using unset
$user_id_to_delete = 44;
unset($user_list[ $user_id_to_delete ]);
// step 3: overwrite 'tmp' post meta with updated array
update_post_meta($post_id, 'tmp', $user_list);
Reading would work like this (only using get_post_meta
):阅读会像这样工作(仅使用
get_post_meta
):
$user_list = get_post_meta($post_id, 'tmp', true);
if ($user_list === false) { // meta wasn't set yet
$user_list = []; // initialize array
}
// iterate over all entries
foreach ($user_id => $user_info) {
echo "User $user_id: ".json_encode($user_info).'<br/>'.PHP_EOL;
}
// read a single user's entry:
$user_id = 32;
echo "User ${user_id}'s entry: ".json_encode( $user_list[$user_id] ).
'<br/>'.PHP_EOL;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.