将字符串字符转换为嵌套数组键


Turning string characters into nested array keys

我正在尝试弄清楚如何将字符串的非零字符转换为嵌套数组键。例如,鉴于$id = '12430'我想以$tree[1][2][4][3]['id'] = '12430'结束.我尝试了几种不同的不成功方法,最新的是:

$id = '12430';
$keys = str_split($id);
function addKey($keys, $id, $array = array())
{
    if ($keys[0] != '0')
    {
        $key = $keys[0];
        $array[$key] = array();
        array_shift($keys);
        addKey($keys, $id, $array[$key]);
    }
    else
    {
        $array['id'] = $id;
        return $array;
    }
}
$tree = addKey($keys, $id);

我做错了什么?或者甚至可能吗?

代码:

class Tree {
    /**
     * @static
     * @access  private
     * @var     array
     */
    private static $aTree = array();
    /**
     * Add single key.
     * 
     * @static
     * @access  public
     * @param   string  $sString
     * @param   array   $aTree
     * @param   string  $sStoredString
     */
    public static function addKey($sString, array &$aTree = NULL, $sStoredString = NULL) {
        if($aTree === NULL) {
            $aTree = &static::$aTree;
        }
        if($sStoredString === NULL) {
            $sStoredString = $sString;
        }
        $sNewKey     = $sString[0];
        $sNewString  = substr($sString, 1);
        if(empty($sNewKey)) {
            $sNewString  = '';
            $aTmp        = &$aTree;
        } else {
            if(!isset($aTree[$sNewKey])) {
                $aTree[$sNewKey] = array();
            }
            $aTmp = &$aTree[$sNewKey];
        }
        if(!empty($sNewString)) {
            static::addKey($sNewString, $aTmp, $sStoredString);
        } else {
            if(!isset($aTmp['id'])) {
                $aTmp['id'] = $sStoredString;
            } else {
                $aTmp['id'] = array($aTmp['id'], $sStoredString);
            }
        }
    }
    /**
     * Add multiple keys.
     * 
     * @static
     * @access  public
     * @param   array   $aKeys
     */
    public static function addKeys(array $aKeys) {
        foreach($aKeys as $sKey) {
            static::addKey($sKey, static::$aTree, $sKey);
        }
    }
    /**
     * Get whole tree.
     * 
     * @static
     * @access  public
     * @return  array
     */
    public static function getTree() {
        return static::$aTree;
    }
}

例:

Tree::addKeys(array('12400', '12430', '233', '2010', '2011'));
Tree::addKey('1111');
echo '<pre>';
print_r(Tree::getTree());
echo '</pre>';

外:

Array
(
    [1] => Array
        (
            [2] => Array
                (
                    [4] => Array
                        (
                            [id] => 12400
                            [3] => Array
                                (
                                    [id] => 12430
                                )
                        )
                )
            [1] => Array
                (
                    [1] => Array
                        (
                            [1] => Array
                                (
                                    [id] => 1111
                                )
                        )
                )
        )
    [2] => Array
        (
            [3] => Array
                (
                    [3] => Array
                        (
                            [id] => 233
                        )
                )
            [id] => Array
                (
                    [0] => 2010
                    [1] => 2011
                )
        )
)

这是你需要的吗?

不确定这是什么变量,但基于问题,这应该使用引用来构建数组。 有人会发布一个递归的:

function addKeys($id) {
    $result = array();
    $temp =& $result;
    foreach(str_split($id) as $key) {
        if($key != 0) {
            $temp =& $temp[$key];
        }
    }
    $temp['id'] = $id;
    return $result;
}
$id = '12430';    
$tree = addKeys($id);    
print_r($tree);