从数据库中的 wordpress 数组中提取 sub_array 值并将其保存回来
Extract sub_array value from wordpress array in the database and save it back
我将数据存储在数组中的自定义字段中
a:5:{i:0;a:1:{s:12:"product_desc";s:44:"Value1";}i:1;a:1:{s:12:"product_desc";s:24:"Value2";}i:2;a:1:{s:12:"product_desc";s:31:"Value3";}i:3;a:1:{s:12:"product_desc";s:41:"Value4";}i:4;a:1:{s:12:"product_desc";s:39:"Value5";}}
我想提取 sub_array product_desc 的值并更新自定义字段 meta_value。
我知道 wordpress 有 maybe_unserialize 来反序列化数据,但是下一步要做什么来提取 sub_array 值?
add_action( 'init', function() {
if ( 'migrate' !== filter_input( INPUT_GET, 'action' ) ) {
return;
}
$query = new WP_Query( [
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'any',
] );
if ( ! $query->have_posts() ) {
return;
}
while ( $query->have_posts() ) {
$query->the_post();
$field_id_1 = 'podu';
$field_value_1 = get_post_meta( get_the_ID(), $field_id_1, true );
$data = maybe_unserialize( $field_value_1 );
//How to extract sub_array values from $data?
}
} );
您的 $data
包含一个数组,因此您可以遍历它:
foreach($data as $singleData){
$desc[] = $singleData['product_desc'];
... now you have your $desc
}
我将数据存储在数组中的自定义字段中
a:5:{i:0;a:1:{s:12:"product_desc";s:44:"Value1";}i:1;a:1:{s:12:"product_desc";s:24:"Value2";}i:2;a:1:{s:12:"product_desc";s:31:"Value3";}i:3;a:1:{s:12:"product_desc";s:41:"Value4";}i:4;a:1:{s:12:"product_desc";s:39:"Value5";}}
我想提取 sub_array product_desc 的值并更新自定义字段 meta_value。
我知道 wordpress 有 maybe_unserialize 来反序列化数据,但是下一步要做什么来提取 sub_array 值?
add_action( 'init', function() {
if ( 'migrate' !== filter_input( INPUT_GET, 'action' ) ) {
return;
}
$query = new WP_Query( [
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'any',
] );
if ( ! $query->have_posts() ) {
return;
}
while ( $query->have_posts() ) {
$query->the_post();
$field_id_1 = 'podu';
$field_value_1 = get_post_meta( get_the_ID(), $field_id_1, true );
$data = maybe_unserialize( $field_value_1 );
//How to extract sub_array values from $data?
}
} );
您的 $data
包含一个数组,因此您可以遍历它:
foreach($data as $singleData){
$desc[] = $singleData['product_desc'];
... now you have your $desc
}