dongxiaoying5882 2012-03-09 00:31
浏览 66
已采纳

PHP - 从分隔的字符串创建多维关联数组

Can you turn this string:

"package.deal.category"

Into an array like this:

$array['package']['deal']['category']

The value inside the index at this point can be anything.

  • 写回答

4条回答 默认 最新

  • dongmao9217 2012-03-09 00:36
    关注

    What have you tried? The absolute answer to this is very easy:

    $keys = explode('.', $string);
    $array = array();
    $arr = &$array;
    foreach ($keys as $key) {
       $arr[$key] = array();
       $arr = &$arr[$key];
    }
    unset($arr);
    

    ...but why would this be useful to you?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?