1

I have a string stored on my database in the following format:

$str = '[0=>"hello",1=>"world"]';

I wish to convert this into an array which would have the 0,"hello" key-value pair, 1,"world" key-value pair.

If there is a better way to format and store the string to make the conversion to array easier I would be open to that answer.

Thanks for your help Jordan

  • 6
    Better way is to build it as an array and then [json_encode()](http://www.php.net/manual/en/function.json-encode.php) it for storage; can easily be restored then using [json_decode()](http://www.php.net/manual/en/function.json-decode.php) – Mark Baker Mar 20 '17 at 19:01
  • 1
    Storing multiple data elements of a row in a single column probably isn't the best idea to begin with. Have you considered adding another table in which each of these items would have its own row, and associating those rows with the row in your original table with a foreign key? – Don't Panic Mar 20 '17 at 19:06
  • I find answer that can helps you, try it: http://stackoverflow.com/questions/3531857/convert-var-dump-of-array-back-to-array-variable – 4EACH Mar 20 '17 at 19:28
  • It would probably be easier to give a good answer if we knew how you were getting that string to begin with. – Don't Panic Mar 20 '17 at 19:31

3 Answers3

0

You can do that by passing the string to eval() but you need to put a return inside your string before the array.

That being said, you're probably better off using JSON format and calling json_decode to decode a JSON string to an array.

Christopher Francisco
  • 15,672
  • 28
  • 94
  • 206
  • Is there any way to store/ retrieve using JSON the index as well? I dont want json to automatically assume the data starts at index 0 and increments along. I want to specify the exact index - is this possible? – user3502651 Mar 20 '17 at 19:33
  • Depending on the database you use, MySQL has a JSON datatype that can be indexed; but better to normalize your database structure – Mark Baker Mar 20 '17 at 19:39
0

Answer for better way part of question, just use json formatted string. Example will be the best I guess. Such snippet of code:

<?php
$str = '{"0":"hello", "1":"world"}';
$arr = json_decode($str);

will give you exact pairs you are asking for.

jedruniu
  • 520
  • 4
  • 13
0

If you want to use a structured data as you are doing now and storing it in a database, you can use a great function that laravel offers you, typecasting your structured data into an array.

Take a look at the documentation at the page https://laravel.com/docs/5.4/eloquent-mutators#array-and-json-casting

The array cast type is particularly useful when working with columns that are stored as serialized JSON. For example, if your database has a JSON or TEXT field type that contains serialized JSON, adding the array cast to that attribute will automatically deserialize the attribute to a PHP array when you access it on your Eloquent model.

This speed up the process for you and sometimes is comfortable than using an external table as suggested.

Simone Cabrino
  • 901
  • 9
  • 24