转换点语法,如“this.that.other";PHP中的多维数组 [英] Convert dot syntax like "this.that.other" to multi-dimensional array in PHP

查看:25
本文介绍了转换点语法,如“this.that.other";PHP中的多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所暗示的那样,我正在尝试创建一个解析器并试图找到将某些内容从点命名空间转换为多维数组的最佳解决方案,以便

Just as the title implies, I am trying to create a parser and trying to find the optimal solution to convert something from dot namespace into a multidimensional array such that

s1.t1.column.1 = size:33%

$source['s1']['t1']['column']['1'] = 'size:33%';

推荐答案

试试这个号码...

function assignArrayByPath(&$arr, $path, $value, $separator='.') {
    $keys = explode($separator, $path);

    foreach ($keys as $key) {
        $arr = &$arr[$key];
    }

    $arr = $value;
}

CodePad

它将遍历键(默认以 . 分隔)以获取最终属性,然后对值进行赋值.

It will loop through the keys (delimited with . by default) to get to the final property, and then do assignment on the value.

如果某些键不存在,则会创建它们.

If some of the keys aren't present, they're created.

这篇关于转换点语法,如“this.that.other";PHP中的多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆