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

4
  • 6
    Better way is to build it as an array and then json_encode() it for storage; can easily be restored then using json_decode() Commented Mar 20, 2017 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? Commented Mar 20, 2017 at 19:06
  • I find answer that can helps you, try it: stackoverflow.com/questions/3531857/… Commented Mar 20, 2017 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. Commented Mar 20, 2017 at 19:31

3 Answers 3

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.

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

2 Comments

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?
Depending on the database you use, MySQL has a JSON datatype that can be indexed; but better to normalize your database structure
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.

Comments

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.

Comments

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.