douchuxun4162 2010-12-23 07:59
浏览 42
已采纳

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

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条回答 默认 最新

  • dqxyh48864 2010-12-23 20:27
    关注

    I was unable to unserialized the string I was passing to the second function via the array segments. I found many posting over the internet indicating that when a serialized string is passed around the core structure of the data that has been passed may have changed even thought it appears intact. I do not know the technicalities. But when I did a var_dump(sunserialized_array) I got false.

    For some of the information I found I post these links:
    http://www.php.net/manual/en/function.unserialize.php#70884
    http://www.php.net/manual/en/function.unserialize.php#40757

    My original goal was to pass a serialized version of an array, as a string element of the same array, to be unserialized and used in the second function. So I resorted to creating a string with the collected keys and variables of the array in questions (array $segments in my question code), and I added the $string_to_be_parsed to the array being passed. I retrieved $string_to_parsed=end($segments) in the second function, and... HERE IS THE GOOD PART use this function to recreate the array parse_str($str, $segments);

    I found this solution: Why does unserialize in PHP keep returning false?

    So, I was not able to unserialized what I wanted, but I found an alternative way of achieving my goal.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘