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

时间:2014-11-13 21:02:34

标签: php arrays string

我试图找出如何将字符串的非零字符转换为嵌套数组键。例如,给定$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);

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

2 个答案:

答案 0 :(得分:0)

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

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);

答案 1 :(得分:0)

代码:

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
                )

        )

)

这是你需要的吗?