转换为关联数组PHP

If you have this data:

1=Books
1.1=Action & Adventure
1.2=Arts, Film & Photography
1.2.1=Architecture
1.2.2=Cinema & Broadcast
1.2.3=Dance

The number on the data is the index. How can you put it in associative array? I want to know an example of associative array with that data. Thanks

Can be done with explode() and foreach

Steps:

1) First explode the string with new line character .

2) Loo over it.

3) You will get individual row, explode() it with =.

4) You will get required key in 0 and value in 1.

5) Store it in array as key value pair. Done

$str = '1=Books
1.1=Action & Adventure
1.2=Arts, Film & Photography
1.2.1=Architecture
1.2.2=Cinema & Broadcast
1.2.3=Dance';
$arr = explode("
", $str);
$assoc = array();
if (! empty($arr)) {
 foreach ($arr as $k => $v) {
  $temp = explode('=', $v);
  $assoc[$temp[0]] = $temp[1];
 }
}
echo '<pre>';print_r($assoc);echo '</pre>';

Output:

Array
(
 [1] => Books
 [1.1] => Action & Adventure
 [1.2] => Arts, Film & Photography
 [1.2.1] => Architecture
 [1.2.2] => Cinema & Broadcast
 [1.2.3] => Dance
)

Can be done by converting string into query string format and then use parse_str() have to insert 1.0 => Books (append.0) and 1.2.0 => Arts, Film & Photography (append .0)

 $str = '1.0=Books
    1.1=Action & Adventure
    1.2.0=Arts, Film & Photography
    1.2.1=Architecture
    1.2.2=Cinema & Broadcast
    1.2.3=Dance';<br />
    //replace & with and because parse_str not work with '&'
    $str = str_replace('&','and',$str);
    $str_ar = explode("
",$str);
    foreach($str_ar as $line){
        $aar .= 'a';
        $line_ar = explode('=',$line);
        $array_index = explode('.',$line_ar[0]);
        foreach($array_index as $index){
            $aar .= '['.$index.']'; 
        }
        $aar.='='.($line_ar[1]).'&';
    }
    $aar = rtrim($aar,'&');
    parse_str($aar,$o);
    $o=array_shift($o);

Output

Array
(
    [1] => Array
        (
            [0] => Books
            [1] => Action and Adventure
            [2] => Array
                (
                    [0] => Arts, Film and Photography
                    [1] => Architecture
                    [2] => Cinema and Broadcast
                    [3] => Dance
                )

        )

)