寻找构建多维、多层、关联数组的方法

因此,我在遍历字符串并将其字符作为索引数组插入多维关联数组时遇到了麻烦。所以基本上是一堆多维数组中的多维数组中的多维数组......这有点麻烦,因为我不能手工完成。我需要一种自动化的方式来用一堆字符串来做到这一点。我认为以下示例将更好地解释它:


//string i want to enter

$string = 'ADAM';

//array i want to end up with

$result = array

              (

                'A'=> array

                 (

                  'D'=>array

                   (

                    'A'=>array

                     (

                      'M'=>array

                        (

                          'result'=>'ADAM'

                        )

                     )

                   )

                 )

               )

我最初的方法只是使用 if 条件将第一个 Char 作为数组插入到主数组中,例如:


for($i = 0; $i < strlen($string); $i++){

      if($i == 0){

       $array1[$word[$i]] = array();

      }

}

效果很好。但后来我遇到了我的问题:我如何跟踪数组中的当前点?在 if-check 之后,我会选择一个 else 语句,当 $i 大于 0 时起作用。但是如果我想插入数组的下一个维度,在这种情况下是 'D',我需要选择$array1['A'],接下来我需要 $array1['A']['D'] 等。我还没有找到一种方法来做到这一点。我需要进入这个数组的字符串从 4-70 个字符不等。我知道的每种方法都只改变了第二维,所以我最终得到:


$array1('A'=>array ('A' =>array()));

$array1('A'=>array ('D' =>array()));

$array1('A'=>array ('A' =>array()));

$array1('A'=>array ('M' =>array()));

或由索引本身是数组引起的非法偏移错误。也许我在这里的方法是不可能的,但我仍然认为我可能会问,以防我错过了什么。


在稍后阶段,我希望对所有字符串使用相同的数组,所以我基本上使用字符作为节点。如果'A'作为第一个字符已经存在,我会跳过它并将下一个字符串的第二个字符插入' A'-阵列等


提前致谢!


holdtom
浏览 108回答 3
3回答

慕娘9325324

您可以为此使用递归函数。function nest(string $str, int $i = 0) {&nbsp; &nbsp; return isset($str[$i]) ? [$str[$i] => nest($str, $i + 1)] : ['result' => $str];}$result = nest($string);

慕标琳琳

这是您要实现的目标的解决方案。请在此处查看有效的解决方案,并附上注释以解释它的工作原理。$val = 'ADAM';$arr = [];$keys = str_split($val);$curr = &$arr;foreach($keys as $key) {&nbsp; &nbsp; $curr = &$curr[$key];}$curr = ['result' => $val];print_r($arr);

森林海

您可以使用引用,以便在遍历字符串时将下一个字符添加为数组的键,然后将此新条目设置为循环中下一个操作的添加点...$string = 'ADAM';$result = [];$add = &$result;for($i = 0; $i < strlen($string); $i++){&nbsp; &nbsp; $add = &$add[$string[$i]];}$add['result'] = $string;print_r($result);只是为了展示添加多个条目是如何工作的......addEntry ( $result, "ADAM");addEntry ( $result, "ALAN");addEntry ( $result, "ADAME");function addEntry ( &$result, string $newValue )&nbsp; {&nbsp; &nbsp; for($i = 0; $i < strlen($newValue); $i++){&nbsp; &nbsp; &nbsp; &nbsp; $result = &$result[$newValue[$i]];&nbsp; &nbsp; }&nbsp; &nbsp; $result['result'] = $newValue;}结果是...Array(&nbsp; &nbsp; [A] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [D] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [A] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [M] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [result] => ADAM&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [E] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [result] => ADAME&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [L] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [A] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [N] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [result] => ALAN&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; ))
打开App,查看更多内容
随时随地看视频慕课网APP