0

I have a string that I need to convert to an array. This is my string that I have in a variable:

$text ='"list_Menu1"=>"root","list_Submenu1"=>"Menu1","list_Submenu2"=>"Menu1","list_Menu2"=>"root",'

And I want to insert it into an array like this:

$tree = array(
    "list_Menu1" => "root",
    "list_Submenu2" => "list_Menu1",
    "list_Submenu3" => "list_Menu1",
    "list_Menu2" => "root",);

I tried to generate the array doing this: $tree = array($text), but it doesn't work. How can I do thus, I'm a little lost.

3
  • $tree = explode(" ,", $text); explode function php.net/manual/en/function.explode.php Commented Nov 5, 2016 at 7:19
  • you can do one thing use str_replace to replace "=>" with ":" in your string then you can use json_decode() to get array from string Commented Nov 5, 2016 at 7:19
  • how about you just create the array yourself, cos i can see it associative too e.g $text = []; $text[] = ['list menu' => $value, 'list_sub => $value] etc Commented Nov 5, 2016 at 7:24

5 Answers 5

2

Try this

    $text ='"list_Menu1"=>"root","list_Submenu1"=>"Menu1","list_Submenu2"=>"Menu1","list_Menu2"=>"root",';

    $text = str_replace("=>",":",$text);
    // trim last coma   
    $text = trim($text,",");
    $text = "{".$text."}";
    $array = json_decode($text,true);

    var_dump($array);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot!!!, this code works great, actually is the shortest and easiest of all :)
2

Its a little long shot, but it works too..

function objectToArray($d) {
    if (is_object($d)) {
        $d = get_object_vars($d);
    }

    if (is_array($d)) {
        return array_map(__FUNCTION__, $d);
    }
    else {
        return $d;
    }
}    

$text ='"list_Menu1"=>"root","list_Submenu1"=>"Menu1","list_Submenu2"=>"Menu1","list_Menu2"=>"root",';

$text = str_replace("=>",':',$text);
$text = rtrim($text,",");
$text = '{'.$text.'}';

$text = json_decode($text);
$text = objectToArray($text);
print_r($text);

2 Comments

This also works great it looks like user3273700 answer, i see that by adding parameter true in $array = json_decode($text,true); you can do it without the use of the function
it wasn't working like that.. that is the reason i have to include objectToArray function..
1

Explode the string by comma (,) and to remove null valued indexes array_filter can be used.

$text ='"list_Menu1"=>"root","list_Submenu1"=>"Menu1","list_Submenu2"=>"Menu1","list_Menu2"=>"root",';
$tree = array_filter( explode(',', $text) );

print '<pre>';
print_r($tree);
print '</pre>';

1 Comment

Hi with this code i get this, but the part of [0],[1] doesnt help me at all Array ( [0] => "list_Menu1"=>"root" [1] => "list_Submenu1"=>"Menu1" [2] => "list_Submenu2"=>"Menu1" [3] => "list_Menu2"=>"root" )
1

Hope this helps:-

<?php

function str_to_arr($str) {
  $str = str_replace('"',"",$str);
  $arr = explode(',',$str);
  for($i=0;$i<count($arr);$i++) {
    if($arr[$i]!="") {
      $tmp_arr = explode('=>',$arr[$i]);
      $arr2[$tmp_arr[0]] = $tmp_arr[1];
    }
  }
  return $arr2;
}

$text  ='"list_Menu1"=>"root","list_Submenu1"=>"Menu1","list_Submenu2"=>"Menu1","list_Menu2"=>"root",';
$arr = str_to_arr($text);
print_r($arr);

?>

Comments

1

Combination of str_replace and explode will do the trick. Here it is:

$text ='"list_Menu1"=>"root","list_Submenu1"=>"Menu1","list_Submenu2"=>"Menu1","list_Menu2"=>"root"';
$new_text = explode(",", str_replace("\"","", $text));
$new_arr_ = array();
foreach($new_text as $values) {
    $new_values = explode("=>", $values);
    $new_arr_[$new_values[0]] = $new_values[1];
}

echo '<pre>';
var_dump($new_arr_);
echo '</pre>';

1 Comment

Hi L. Herrera, this code also works thanks a lot for your help :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.