用数组更新 post 元数据而不嵌套
Update post meta with array without being nested
如果将逗号分隔数组转换为普通数组并更新 wordpress 中的 post 元数据,我会得到一个不需要的额外嵌套数组层。有谁知道如何解决这一问题?或者在更新 post 元数据之前转换数组?
$array_1 = array (
0 => '6801,6800,7310,6795',
);
$array_2 = array();
foreach ($array_1 as $value) {
array_push($array_2 , explode(",",$value));
}
update_post_meta($post->ID, 'post_meta_field', $array_2);
//print_r($array_2);
}
add_action( 'save_post', 'my_save_post_function', 10, 3 );
输出如下:
array (
0 => // I don't need this layer!!
array (
0 =>
array (
0 => '6801',
1 => '6800',
2 => '7310',
3 => '6795',
),
),
)
但我需要这个:
array (
0 =>
array (
0 => '6801',
1 => '6800',
2 => '7310',
3 => '6795',
),
)
假设您需要所有逻辑来在实际代码中生成数组,您可以简单地挑选出最外层数组的第一个元素:
update_post_meta($post->ID, 'post_meta_field', $array_2[0]);
如果将逗号分隔数组转换为普通数组并更新 wordpress 中的 post 元数据,我会得到一个不需要的额外嵌套数组层。有谁知道如何解决这一问题?或者在更新 post 元数据之前转换数组?
$array_1 = array (
0 => '6801,6800,7310,6795',
);
$array_2 = array();
foreach ($array_1 as $value) {
array_push($array_2 , explode(",",$value));
}
update_post_meta($post->ID, 'post_meta_field', $array_2);
//print_r($array_2);
}
add_action( 'save_post', 'my_save_post_function', 10, 3 );
输出如下:
array (
0 => // I don't need this layer!!
array (
0 =>
array (
0 => '6801',
1 => '6800',
2 => '7310',
3 => '6795',
),
),
)
但我需要这个:
array (
0 =>
array (
0 => '6801',
1 => '6800',
2 => '7310',
3 => '6795',
),
)
假设您需要所有逻辑来在实际代码中生成数组,您可以简单地挑选出最外层数组的第一个元素:
update_post_meta($post->ID, 'post_meta_field', $array_2[0]);