Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using explode function with a variable in the string?

Tags:

php

I have an array with $post_id as keys. When save the $data, I saved it as a string:

foreach( $data as $post_id => $details )
  $string .= "-pid-$post_id-$details";

When use the data, I need to convert it back to array with $post_id as key and $details as value. How to explode it when I don't know what is the $post_id ?

like image 308
Jenny Avatar asked Aug 24 '26 09:08

Jenny


1 Answers

Don't do it this way. if you need to serialize a string use json_encode():

$string = json_encode($data);

Then, when you need to decode it again:

$data = json_decode($string);

Safe and easy.

Here's the PHP reference: json_encode()


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!