How to convert array to a string using methods other than JSON?

后端 未结 8 2152
臣服心动
臣服心动 2020-12-01 13:32

What is a function in PHP used to convert array to string, other than using JSON?

I know there is a function that directly does like JSON. I just don\'t remember.

8条回答
  •  臣服心动
    2020-12-01 14:14

    serialize() is the function you are looking for. It will return a string representation of its input array or object in a PHP-specific internal format. The string may be converted back to its original form with unserialize().

    But beware, that not all objects are serializable, or some may be only partially serializable and unable to be completely restored with unserialize().

    $array = array(1,2,3,'foo');
    echo serialize($array);
    
    // Prints
    a:4:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;s:3:"foo";}
    

提交回复
热议问题