1

I have a string like "create:this:assoc:array"

I want to create from this string

$array["create"]["this"]["assoc"]["array"] = "data"

what i have is

static public function add($data_path, $data) 
{
    if(!empty($data) && !empty($data_path)) {
        $keys = explode(":", $data_path);
        $looper = array();
        $length = count($keys) - 1;
        for ($i=0; $i <= $length ; $i++) 
        {
            if($i == $length)
                @$looper[$keys[$i]] = $data;
            else
            {

                @$looper[$keys[$i]] = $keys[$i+1];
            }
        }// end for loop
                echo '<pre>';
                print_r($looper);
                echo '</pre>';
    }
}
4

1 回答 1

4

尝试这样的事情

 $s = "create:this:assoc:array";
 $array = array_reverse(explode(":", $s));
 $result = "data";
 foreach($array as $key => $value){
      $result = array($value => $result);
 }
 print_r($result);
于 2013-09-23T19:10:46.293 回答