Question

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
Was it helpful?

Solution 2

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.

OTHER TIPS

hey DOD this code work correctly !



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);



Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top