I have create a custom meta key into the post table and it's value is in serialized form now when I dump the value it is giving me like this
Array ( [0] => a:3:{s:10:"product_id";s:4:"2592";s:7:"user_id";i:41;i:0;a:1:{i:0;s:63:"a:3:{s:10:"product_id";s:4:"2592";s:7:"user_id";i:2;i:0;a:0:{}}";}} )
I want to get user_id and product_id from it. The main idea is to hide a button for those user whose ID's are stored into this meta_key for the particular product whose ID are in this serialized array as well any idea how to do that I have tried the following code but nothing happens
$next_in_line = get_post_meta(esc_attr($product->get_id()), '_next_in_line');
print_r($next_in_line);
$unserilize_data = unserialize($next_in_line);
print_r($unserilize_data['0']);
But is giving me bool(false) and when I use this function
$next_in_line = get_post_meta(esc_attr($product->get_id()), '_next_in_line');
print_r($next_in_line);
$un_data = maybe_unserialize($next_in_line);
print_r($un_data['0']);
it is returning me values in the following format
a:3:{s:10:"product_id";s:4:"2592";s:7:"user_id";i:41;i:0;a:1:{i:0;s:63:"a:3:{s:10:"product_id";s:4:"2592";s:7:"user_id";i:2;i:0;a:0:{}}";}}
Now how can I make the condition that I have mentioned above I am blank at this point please some one help me out. Thank you in advance
UPDATE
I am storing data like this
function nextInLine(){
$product_id = $_POST['product_id'];
$user_id = get_current_user_id();
$next_customer_meta = get_post_meta($product_id, '_next_in_line');
if($next_customer_meta == ''){
$meta_array = array(
'product_id' => $product_id,
'user_id' => $user_id
);
$meta_value = maybe_serialize($meta_array);
} else {
$meta_array = array(
'product_id' => $product_id,
'user_id' => $user_id
);
array_push($meta_array, $next_customer_meta);
$meta_value = maybe_serialize($meta_array);
}
var_dump($meta_value);
update_post_meta($product_id, '_next_in_line', $meta_value);
return true;
}
add_action('wp_ajax_nextInLine', 'nextInLine');
add_action('wp_ajax_nopriv_nextInLine', 'nextInLine');
update_post_meta
does that for you.