convert array to string and get it back to array in PHP

前端 未结 2 643
情深已故
情深已故 2021-01-23 09:25

I\'m using the Serialize function to store an array in my MYSQL database, and then I\'m unSerialize Him in other page. The array structure look like this :

Array         


        
2条回答
  •  渐次进展
    2021-01-23 10:09

    Try json_encode and json_decode

    $array_to_store = array(....);
    $str_array = json_encode($array_to_store);
    //Store $str_array
    //Retrieve it and to make it an array again
    $array = json_decode($str_array, true);
    
    ******************* Edited *********************
    

    I do not see what is wrong with serialize and unserialize:

    $array = array(
            array(
                    'names' => 'somename1',
                    'rating' => 10
                 ),
            array(
                    'names' => 'somename2',
                    'rating' => 9
                 )
        );
    
    //Array before
    print_r('
    ');
    print_r($array);
    
    //Serialised Array
    $s = serialize($array);
    
    print_r('
    ');
    print_r($s);
    
    //Unserialised Array
    print_r('
    ');
    print_r(unserialize($s));
    

提交回复
热议问题