Title: Serialize Javascript array as PHP string for Laravel cache
Tags: javascript;php;laravel;serialization;redis
Question: I need to save a PHP array as serialized data suitable for Laravel's cache system. My code so far using node-php-serialization is:

```var serialize=require("php-serialization").serialize;
var sernew = serialize(result, "string");
```

This produces:

```"s:11:"bob,dave,mark";"
```

But this doesn't work in Laravel's cache. I need:

```"s:27:\"[\"bob\",\"dave\",\"mark\"]\";"
```

Can this package do this? Does anyone know of a way it can be formatted as above?

Many thanks.

Additional details:

The array to be inserted comes from the cache, is converted to Javascript array, altered, then converted back to the cache string:

```rediscache.get(key, function (err, result) {
      result = unserialize(result);
      result = JSON.parse(result);

      for(var i = result.length - 1; i &gt;= 0; i--) {
          if(result[i] === username) {
             result.splice(i, 1);
          }
      }

      var sernew = serialize(result, "string");

});
```


Here is the accepted answer: The result you want - ```"s:27:\"[\"bob\",\"dave\",\"mark\"]\";"``` - is a JSON string ```["bob","dave","mark"]```.

What you insert back to serialize is just a string. In this exact case, doing this will work:

```var sernew = serialize(JSON.stringify(("bob,dave,mark").split(",")), "string");
```

But it might not work in all cases if you're not just having a simple array.

Can you show the full path of what are you doing with the string. Something might be incorrectly done.

Comment for this answer: I've added some additional details. I'll look into converting the array back to JSON...
Comment for this answer: The result that you get after doing the splicing is an array. You should be turning that array back to a JSON string with JSON.stringify() as I've mentioned and only then serializing. `var sernew = serialize(JSON.parse(result), "string");`


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:11/10/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<javascript><php><laravel><serialization><redis>', 'Title': 'Serialize Javascript array as PHP string for Laravel cache', 'ViewCount': 677, 'id': 33628230, 'user_AboutMe': '', 'user_AccountId': 991351.0, 'user_DisplayName': 'samiles', 'user_Reputation': 3608.0, 'user_Views': 451.0, 'user_id': 1008563.0}}