寻找一种构造多维多层关联数组的方法 [英] Searching for a way to construct a multidimensional, multilayered, associative Array

查看:72
本文介绍了寻找一种构造多维多层关联数组的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在遍历字符串并将其chars作为索引数组插入多维关联数组时遇到麻烦.所以基本上是一维多维数组中的一维多维数组.这有点麻烦,因为我无法手动完成.我需要一种自动化的方法来处理一堆字符串.下面的示例将对它进行一些解释,我认为:

So i am running into trouble while iterating through a string and inserting it's chars as index-arrays into a multidimensional,associative array. So basically a bunch of multidimensional arrays in multidimensional arrays in multidimensional arrays..... That is a bit troublesome as i cant do it by hand. I need an automated way to do this with a bunch of strings. The following example will explain it a bit better i think:

//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-conditions将第一个Char作为数组插入到主数组中,例如:

my initial approach was just using if-conditions to insert the first Char as Array into the main-array like:

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个字符.我所知道的每种方法都只改变了第二维,所以我最终得到了:

which works pretty well. But then i ran into my Problem: how do i keep track of the current point in the array? Following the if-check i would go for an else-statement that acts when $i is bigger than 0. But if i want to insert the next dimension of array, which would be 'D' in this case, i would need to select $array1['A'], for the next i would need $array1['A']['D'] etc. I wasn't able to find a way yet to do that. The strings i need to get into this array vary from 4-70 chars. Each method i know of only changed the 2nd dimension so i ended up with:

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

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

or illegal offset-errors caused by the indexes being arrays themselves. Maybe my approach here is not possible, but i still thought i might ask in case i missed something.

在稍后阶段,im希望对所有字符串使用相同的数组,因此我基本上将Chars用作节点..如果已经存在作为第一个char的'A',那么我将跳过它并插入下一个字符串的第二个char放入"A"数组等.

In a Later stage im looking to use the same array for all strings so i basically use the Chars as nodes.. if 'A' as first char already exists i would then skip that and insert the 2nd char of the next string into the 'A'-Array etc.

预先感谢!

推荐答案

您可以为此使用递归函数.

You can use a recursive function for this.

function nest(string $str, int $i = 0) {
    return isset($str[$i]) ? [$str[$i] => nest($str, $i + 1)] : ['result' => $str];
}

$result = nest($string);

这篇关于寻找一种构造多维多层关联数组的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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