3

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 ?

1
  • 1
    Just explode on "-pid-"? Commented Jun 28, 2013 at 3:48

4 Answers 4

5

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

Sign up to request clarification or add additional context in comments.

Comments

3

php has a method called serialize which will take an array (such as $_POST) and convert it to a string that can then be recreated into an array with unserialize

<?php
    // $_POST looks like this for example:
    // $_POST['value'] = 100;
    $string = serialize($_POST);
    echo $string; // Prints '"a":1:{s:5:"value";s:3:"100";}'
    $data = unserialze($string);
    print_r($data); // Prints Array[0] ( 'value' => '100' )
?>

I won't lecture about not sanitizing user input, but SANITIZE USER INPUT.

serialize

unserialize

Note that this is a binary string which may include null bytes, and needs to be stored and handled as such. For example, serialize() output should generally be stored in a BLOB field in a database, rather than a CHAR or TEXT field. -- from php docs

2 Comments

serialize() produces a binary string that may contain null bytes, amongst other things. It won't necessarily be safe in places where a JSON string will.
@MikeW added to note to answer.
2

You would need to explode the string using

explode ( string $delimiter , string $string [, int $limit ] )

This will return your data as an array

for example

foreach($data AS $post_id => $details) {
  $string .= "|||$post_id||$details"
}

then to get your data back out of string

    $newArray = explode('|||', $string);

foreach($newArray AS $key=>$val){
   $holding = explode('||', $val);
   $finalArray[$holding[0]] = $val;
}

Now you will have an array with key being the id and val being the details for each of the items in the string.

EDIT:

Or use serialize and unserialize like Brombomb suggested.

Comments

1

If I understand your question...

$temp = explode('-pid-', $string);
array_shift($temp);

foreach ($temp as $item) {
    list($post_id, $value) = explode('-', $item);
    $data[$post_id] = $value;
}

json_encode and associated json_decode may be a better option for "stringifying" your data in the first place. However, there may be a legitimate reason for doing it the way you've chosen to do it.

2 Comments

what if there's a - in the post_id?
Then you're screwed because there are no other delimiters to go off of.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.