Thea
Thea

Reputation: 27

Create an array from content in string in PHP

I have a string that takes multiple key values and i would like to get these value and create an array from it.

The string will be built the same way but might contain more keys or less keys. An example of a string is this

"title:'this is the new title', msg:'this_is_updated', body:'this is the new body text'

So i need some way to to turn these keys from the string into an array that looks like this

$array['customMessage'] = [
  'title' => 'this is the new title',
  'msg' => 'this_is_updated',
  'body' => 'this is the new body'
];

Upvotes: 0

Views: 77

Answers (5)

hatsagorts
hatsagorts

Reputation: 48

The string is a simple JSON object, just decode with this native function

$array = json_decode($string)

Upvotes: 0

Burhan Ibrahimi
Burhan Ibrahimi

Reputation: 377

if its allways in this format

key: val, key:val

as you have showed then use explode

$str = "title:'this is the new title', msg:'this_is_updated', body:'this is the 
new body text'";
foreach(explode(',', $str) as $val)
{
    $item = explode(':', $val);
    $array['customMessage'][trim($item[0])] = trim($item[1],"'");
}

Upvotes: 2

Jeppsen
Jeppsen

Reputation: 497

If possible, you can build the string in a JSON format and use json_decode() like this:

$json = '{
    "title":"this is the new title",
    "msg":"this_is_updated",
    "body":"this is the new body text"
}';

$array = json_decode($json, true);

$arrayFinal = array();
$arrayFinal['customMessage'] = $array;

echo '<pre>';
print_r($arrayFinal);
echo '</pre>';

This will give the following output:

Array
(
    [customMessage] => Array
        (
            [title] => this is the new title
            [msg] => this_is_updated
            [body] => this is the new body text
        )

)

Upvotes: 0

Nageen
Nageen

Reputation: 1759

this is the logic

$str = "title:'this is the new title', msg:'this_is_updated', body:'this is the new body text' ";
        $str1 = explode(',', $str);
        $array['customMessage'] = array();
        foreach ($str1 as $key => $value) {
            $str2 = explode(':', $value);

            $array['customMessage'][$str2[0]] = $str2[1];
        }


        print_r($array['customMessage']);die;

Upvotes: 0

Realit&#228;tsverlust
Realit&#228;tsverlust

Reputation: 3953

First of all, you should always try to use an already existing format. JSON is perfect for that, for example, and PHP has already existing functions to work with that.

If that's not possible, for whatever reason, you can use the following to achive your result string:

$string = "title:'this is the new title', msg:'this_is_updated', body:'this is the new body text'";

$firstExplode = explode(',', $string);

foreach($firstExplode as $explode) {
  $val = explode(':', $explode);
  $arr[$val[0]] = $val[1];
}

var_dump($arr);

Output:

array(3) {
  ["title"]=>
  string(23) "'this is the new title'"
  [" msg"]=>
  string(17) "'this_is_updated'"
  [" body"]=>
  string(27) "'this is the new body text'"
}

Upvotes: 1

Related Questions