---
title: "How can I convert this string to array?"  
description: "How can I convert this string to array?"  
author: "Anonymous User"  
published: 2013-10-05  
updated: 2013-10-05  
canonical: https://www.mindstick.com/forum/1593/how-can-i-convert-this-string-to-array  
category: "json"  
tags: ["json"]  
reading_time: 2 minutes  

---

# How can I convert this string to array?

I am stucked with converting the [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) of specific [format](https://www.mindstick.com/forum/162083/how-to-convert-ost-files-into-pst-format) to [array](https://www.mindstick.com/articles/335/jagged-array-in-c-sharp-dot-net). Spliting the string using explodedoesn't seems to be the [right approach](https://answers.mindstick.com/qa/37397/what-is-the-right-approach-to-java-programming) and i am not so good with [regular expressions](https://www.mindstick.com/forum/159366/how-to-test-regular-expressions-in-sql-without-using-database-table). So my [question](https://www.mindstick.com/blog/23175/how-to-solve-neet-question-paper-in-less-time) is how can i [convert](https://www.mindstick.com/forum/2093/configurationmanager-appsettings-convert-n-to-n-why) the following string to array?

Current format of the string

\

```
maxWidth: 800,
openEffect: elastic,
closeEffect: elastic,
helpers : {
       title : {
             type: outside
           },
       thumbs : {
              width : 50,
              height : 50
            }
      }
```

\

## Desired Array

```
array(
  'maxWidth' => 800,
  'openEffect' => 'elastic',
  'closeEffect' => 'elastic',
  'helpers' => array(
               'title' => array('type' => 'outside'),
               'thumbs' => array('width' => 50, 'height' => 50)
             )
)
```

\

Any help would be greatly appreciated.

## Replies

### Reply by E E Cummings

The string in your example is almost a valid JSON (JavaScript Object Notation) structure!

Here's what your string would look like as valid JSON

```
 {
    "maxWidth": 800,
    "openEffect": "elastic",
    "closeEffect": "elastic",
    "helpers": {
        "title": {
            "type": "outside"
        },
        "thumbs": {
            "width": 50,
            "height": 50
        }
    }
}
```

So our [approach](https://www.mindstick.com/interview/985/what-are-the-approaches-that-you-will-follow-for-making-a-program-very-efficient) (as suggested by @WiseGuy) would be to first inject a few characters with preg_replace to Turn your string init into valid JSON:

```
$str = preg_replace('/\b/' , '"' , $str);
$str = '{'  . $str . '}';
```

The regex above is using the Word Boundaries anchor to add quotation marks around all words. Then we wrap the whole thing in curly braces and voilà, we've got a x-language compatible object format.\


---

Original Source: https://www.mindstick.com/forum/1593/how-can-i-convert-this-string-to-array

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
