取消序列化从php上的db获得的值

取消序列化从php上的db获得的值,php,wordpress,serialization,Php,Wordpress,Serialization,我需要做一些事情,我相信这是简单的,但我阅读和阅读信息无处不在,无法找到解决这个问题的办法。我知道,我是个笨蛋,但我真的不知道现在还能尝试什么 我的网站主题的一个php文件上有一行,如下所示: global $post; if (!is_page()) {$category_ID = get_post_meta($post->ID, '_category_permalink', true);} else {$category_ID = '';} $category_paren

我需要做一些事情,我相信这是简单的,但我阅读和阅读信息无处不在,无法找到解决这个问题的办法。我知道,我是个笨蛋,但我真的不知道现在还能尝试什么

我的网站主题的一个php文件上有一行,如下所示:

global $post;
if (!is_page()) {$category_ID = get_post_meta($post->ID, '_category_permalink', true);}
    else {$category_ID =  '';}  
$category_parent = pa_category_top_parent_id ($category_ID);
a:1:{s:8:"category";s:2:"23";}
问题是数据库上的“\u category\u permalink”值并不总是整数,我需要它。有时该值是一个序列化值,如下所示:

global $post;
if (!is_page()) {$category_ID = get_post_meta($post->ID, '_category_permalink', true);}
    else {$category_ID =  '';}  
$category_parent = pa_category_top_parent_id ($category_ID);
a:1:{s:8:"category";s:2:"23";}
所以,$category_ID有时是23(或任何其他数字),其他时间是a:1:{s:8:“category”;s:2:“23”}(以及其他值)

我需要做的是,检查该行之后获得的值是否为整数。如果是,我需要将其反序列化,并将其转换为相应的整数,存储在序列化值的“category”部分(从我的示例中,数字23),因此$category_ID始终等于整数,而不是序列化值

我怎么做

提前感谢:)

编辑和更新:

这已经解决了,代码就这样结束了

if (!is_page()) { $category_ID = get_post_meta($post->ID, '_category_permalink', true); } else {$category_ID =  '';}    
global $wpdb;
$catmeta = $wpdb->get_row("SELECT post_id, meta_key, meta_value FROM MYDATABASENAMEHERE WHERE post_id = '".$post->ID."' AND meta_key LIKE '_category_permalink'",ARRAY_A);

if(strpos($catmeta['meta_value'],':{')!==false){
$catmeta = unserialize($catmeta['meta_value']);
$catmeta['meta_value'] = $catmeta['category'];
}

$category_parent = pa_category_top_parent_id ($catmeta['meta_value']);

首先使用
is\u number()
检查
$category\u ID
是否为数字

如果不是,则使用
unserialize()
将其转换为
array
并选中
category

if (is_numeric($category_ID)) {
     # $category_ID is number, use it
     # your code here...
} else {
    # $category_ID is a serialized value
    $cat = unserialize($category_ID);
    $category_ID = intval($cat['category']);
}

首先使用
is\u number()
检查
$category\u ID
是否为数字

如果不是,则使用
unserialize()
将其转换为
array
并选中
category

if (is_numeric($category_ID)) {
     # $category_ID is number, use it
     # your code here...
} else {
    # $category_ID is a serialized value
    $cat = unserialize($category_ID);
    $category_ID = intval($cat['category']);
}
别担心。 您可以通过以下代码检查此编号是否为:

//check if is numeric
 if(is_numeric($category_parent)){
     $category_parent_id = $category_parent;
    } else {
    //if is array unserialize
     $category_parent_array = unserialize($category_parent);
     //use array to get the id
     $category_parent_id = $category_parent_array['category'];
    }
别担心。 您可以通过以下代码检查此编号是否为:

//check if is numeric
 if(is_numeric($category_parent)){
     $category_parent_id = $category_parent;
    } else {
    //if is array unserialize
     $category_parent_array = unserialize($category_parent);
     //use array to get the id
     $category_parent_id = $category_parent_array['category'];
    }
使用以下代码:

$data = unserialize('a:1:{s:8:"category";s:2:"23";}');
$category = $data['category'];
if( is_numeric($category) ){
    //Write code here if category is numeric (integer)
}else{
    //Write code here if category is string (not numeric)
}
使用以下代码:

$data = unserialize('a:1:{s:8:"category";s:2:"23";}');
$category = $data['category'];
if( is_numeric($category) ){
    //Write code here if category is numeric (integer)
}else{
    //Write code here if category is string (not numeric)
}

您可以通过
is\u numeric()
检查数值是否为数值。您可以通过
is\u numeric()
检查数值是否为数值。问题是数值并不总是相同的,因此我不能这样添加它$str可以是整数,也可以是序列化的值。序列化格式是相同的,但不是相同的精确值。@shinji第三次检查
$category\u ID
是否是我在回答中提到的第一个数字。如果它是一个数字,那么就使用它。否则,继续执行
unserialize()
。请参阅我的更新答案OK,is_编号不起作用。相反,该命令是数字的。但是,尽管从逻辑上讲,它应该是有效的,但在这种情况下,它并没有起作用。最后,经过大量调查,我找到了一个解决办法。我的原始邮件已被编辑以包含它。谢谢你的帮助!问题是这个值并不总是相同的,所以我不能这样添加它$str可以是整数,也可以是序列化的值。序列化格式是相同的,但不是相同的精确值。@shinji第三次检查
$category\u ID
是否是我在回答中提到的第一个数字。如果它是一个数字,那么就使用它。否则,继续执行
unserialize()
。请参阅我的更新答案OK,is_编号不起作用。相反,该命令是数字的。但是,尽管从逻辑上讲,它应该是有效的,但在这种情况下,它并没有起作用。最后,经过大量调查,我找到了一个解决办法。我的原始邮件已被编辑以包含它。谢谢你的帮助!这段代码看起来很有希望,我试过了,但没有成功。不过,我对它做了一些修改,因为我需要检查的值是$category\u ID而不是$category\u parent,但奇怪的是,它似乎不起作用。这段代码看起来很有希望,我尝试了它,但它不起作用。不过我对它做了一些修改,因为我需要检查的值是$category\u ID而不是$category\u parent,但这很奇怪,因为它似乎不起作用。