无法将数组元素反序列化为数组变量

时间:2010-12-23 07:59:17

标签: php arrays serialization urldecode

I am passing this array from one function to another


    function componentBuildRoute(&$query)
    {
        $segments = array();
        $segments[] = $var...
        ...
        $str = serialize($segments);
        $str = urlencode($str);
        $segments[] = $str;

//if I do a pre print_r for $segments[];
//THIS PRINTS
//**SERIALIZED:
//Array
//(
//    [page] => mostrar_clasificado
//    [catid] => 3
//    [category_alias] => nuevo
//    [adid] => 3
//    [ad_alias] => 3-way-conector-de-tubos
//    [0] => a%3A5%3A%7Bs%3A4%3A%22page%22%3Bs%3A19%3A%22mostrar_clasificado%22%3Bs%3A5%3A%22catid%22%3Bs%3A1%3A%223%22%3Bs%3A14%3A%22category_alias%22%3Bs%3A5%3A%22nuevo%22%3Bs%3A4%3A%22adid%22%3Bs%3A1%3A%223%22%3Bs%3A8%3A%22ad_alias%22%3Bs%3A23%3A%223-way-conector-de-tubos%22%3B%7D
//)**

    return $segments;
    }
   
 function componentParseRoute( $segments )  
    {
//if I do a pre print_r() for the passed array of $segments[]; 
//*THIS PRINTS
//**PASSED SEGMENTS:
//Array
//(
//    [0] => mostrar_clasificado
//    [1] => 3
//    [2] => nuevo
//    [3] => 3
//    [4] => 3:way-conector-de-tubos
//    [5] => a:5-{s-4-"page";s-19-"mostrar_clasificado";s-5-"catid";s-1-"3";s-14-"category_alias";s-5-"nuevo";s-4-"adid";s-1-"3";s-8-"ad_alias";s-23-"3-way-conector-de-tubos";}
//)**
        $str = end($segments);
        echo "LAST ELEMENT OF PASSED ARRAY STR:
        ".**$str**; 

//THIS PRINTS:
//LAST ELEMENT OF PASSED ARRAY STR:
a:5-{s-4-"page";s-19-"mostrar_clasificado";s-5-"catid";s-1-"3";s-14-"category_alias";s-5-"nuevo";s-4-"adid";s-1-"3";s-8-"ad_alias";s-23-"3-way-conector-de-tubos";}
//**DECODED ALREADY HOW COME?**
        $testing = array();
        **$testing = unserialize($str);**
        echo "UNSERIALIZED TESTING:";
    print_r($testing);
//THIS PRINTS:
//UNSERIALIZED SEGMENTS: 
//**NO ARRAY!!! (BLANK SPACE AFTER TEXT) NO UNSERIALIZED ARRAY**
//I'LL PRINT THE ARRAY AGAIN TO VERIFY I GOT WHAT I THOUGHT I GOT

echo "SEGMENTS 2:";
    print_r($segments);

//THIS PRINTS:
//SEGMENTS 2:
//
//Array
//(
//    [0] => mostrar_clasificado
//    [1] => 3
//    [2] => nuevo
//    [3] => 3
//    [4] => 3:way-conector-de-tubos
//    [5] => a:5-{s-4-"page";s-19-"mostrar_clasificado";s-5-"catid";s-1-"3";s-14-"category_alias";s-5-"nuevo";s-4-"adid";s-1-"3";s-8-"ad_alias";s-23-"3-way-conector-de-tubos";}
//)
//I AM UNABLE TO **UNSERIALIZED** THE LAST ELEMENT OF THE ARRAY
//any pointers?
//also, as I mentioned before, 
//I don't understand why the urlencoded 
//and serialized 
//element of the array, gets passed to 
//the second function already urldecoded

2 个答案:

答案 0 :(得分:0)

嘿DOD这段代码正常工作!



function componentBuildRoute(&$query)
{
        $segments = array();
        $str = serialize(
            array(
                "page"=>"mostrar_clasificado",
                "catid"=>"3",
                "category_alias"=>"nuevo",
                "adid"=>"3",
                "ad_alias"=>"3-way-conector-de-tubos"
                )
            );
        $str = urlencode($str);
        $segments[] = $str;
        return $segments;
}
function componentParseRoute( $segments )  
{
        $str = end($segments);
        $testing = array();
        $str = urldecode($str);
        $testing = unserialize($str);
   return $testing;
}

$query = '';

$val = componentBuildRoute(&$query);
print_r($val);
echo "\n";
$val2 = componentParseRoute($val);
print_r($val2);



答案 1 :(得分:0)

我无法通过数组段将我传递给第二个函数的字符串反序列化。我在互联网上发现了许多帖子,表明当一个序列化的字符串传递给已传递的数据的核心结构时,即使它看起来完好无损也可能已经改变。我不知道技术性。但是当我做了var_dump(sunserialized_array)时,我得到了错误。

对于我发现的一些信息,我发布了这些链接:
http://www.php.net/manual/en/function.unserialize.php#70884
http://www.php.net/manual/en/function.unserialize.php#40757

我最初的目标是传递一个数组的序列化版本,作为同一数组的字符串元素,以便在第二个函数中进行反序列化和使用。所以我在问题中使用收集的键和数组变量创建了一个字符串(我的问题代码中的数组$ segments),并且我将$ string_to_be_parsed添加到正在传递的数组中。我在第二个函数中检索了$ string_to_parsed = end($ segments),并且...这里是好的部分使用此函数重新创建数组 parse_str($ str,$ segments);

我找到了这个解决方案:Why does unserialize in PHP keep returning false?

所以,我无法对我想要的内容进行反序列化,但我找到了另一种实现目标的方法。