从字符串爆炸中递归创建多维数组

我有一个包含很多类别字符串的数组,如</ p>

  汽车&gt;  Model1 
Cars&gt; 模型1&gt; 配件
汽车&gt; 模型1&gt; 东西
汽车&gt; Model2
Cars&gt; Model3
</ code> </ pre>

我希望递归转换为多维数组,其中“Cars”是Model1,Model2和Model3的父元素。</ p>
\ n

当然,“附件”和“Something”将是Model1的子元素。</ p>

一直试图弄清楚这背后的逻辑很长一段时间了, 我似乎无法弄明白...... </ p>

编辑:到目前为止,我所拥有的并不多。 如上所述,似乎无法弄清楚如何开发它的逻辑。</ p>

  $ array = array(
'Cars&gt; Model1',
'Cars &gt; Model1&gt; Accessories',
'Cars&gt; Model1&gt; Something',
'Cars&gt; Model2',
'Cars&gt; Model3'
);

for($ i = 0; $ i&lt; count($ array); $ i ++){
$ name = explode('&gt;',$ array [$ i]);

//以某种方式检查当前的“名称” 已被推送到新数组 - 如果是,则添加为子项。
}
</ code> </ pre>
</ div>

展开原文

原文

I have an array containing a lot category strings like

Cars > Model1
Cars > Model1 > Accessories
Cars > Model1 > Something
Cars > Model2
Cars > Model3

which I want to recursively transform into a multidimensional array where "Cars" is the parent element of Model1, Model2 and Model3.

And of course, "Accessories" and "Something" would be child elements of Model1.

Have been trying to work out the logic behind this for quite some time now, and I just can't seem to figure it out...

Edit: What I have so far, it's not much. As mentioned, can't seem to figure out the logic behind how to develop this.

$array = array(
        'Cars > Model1',
        'Cars > Model1 > Accessories',
        'Cars > Model1 > Something',
        'Cars > Model2',
        'Cars > Model3'
    );

    for ($i = 0; $i < count($array); $i++) {
        $name = explode('>', $array[$i]);

        // somehow check if the current "name" is already pushed to a new array - if it is, then add as a child.
    }

douli1872
douli1872 如果有帮助,请查看我的答案..
4 年多之前 回复
dpg78570
dpg78570 添加了我迄今为止的一小段代码。@MateiMihai,我认为这个解决方案不会为我解决。我需要“汽车”作为一个独特的键,并在其下添加不同的“模型”作为一个多维数组。
4 年多之前 回复
duanhan5388
duanhan5388 包含路径作为多维数组字符串的变量的可能重复?
4 年多之前 回复
douxu5845
douxu5845 我试过爆炸“>”并循环遍历爆炸的字符串,将其推入$newArray。但是在每次迭代中,我需要检查这个特定的名字是否已经在$newArray中-因为那样我们就不会添加它了。
4 年多之前 回复
donglu1913
donglu1913 发布你的PHP代码
4 年多之前 回复

1个回答



以下是如何实现此目的</ p>

  $ array_data = array(
' 汽车&gt; Model1',
'汽车&gt; Model1&gt;配件',
'汽车&gt; Model1&gt; Something',
'汽车&gt; Model2',
'汽车&gt; Model3'
) ;

$ last_key = null;
$ array = [];
$ result = [];
foreach($ array_data as $ key =&gt; $ values)
{
$ data = explode( '&gt;',$ values);
if(is_array($ data))
{
$ count = 0;
$ pop = array_pop($ data);
foreach($ data as $ k = &gt; $ v)
{
if if($ count == 1)
{
unset($ result [$ last_key]);
$ result [$ last_key] [$ v] = $ pop; \ n $ count ++;
continue;
}
$ last_key = $ v;
$ result [$ v] = $ pop;
$ count ++;
}
$ array [] = $ result; \ n}
}
</ code> </ pre>

结果将类似于</ p>

  echo'&lt; pre&gt;'  ; print_r($ array); 
Array

[0] =&gt;数组

[汽车] =&gt;模式 l1

[1] =&gt; 数组

[Cars] =&gt;数组

[模型1] =&gt;附件

[2] =&gt; 数组

[Cars] =&gt;数组

[模型1] =&gt;某事物

[3] =&gt; 数组

[Cars] =&gt; Model2

[4] =&gt; 数组

[汽车] =&gt;模型3


</ </ code> </ pre>
</ div>

展开原文

原文

Here's how you can achieve this

$array_data = array(
    'Cars > Model1',
    'Cars > Model1 > Accessories',
    'Cars > Model1 > Something',
    'Cars > Model2',
    'Cars > Model3'
);

$last_key = null;
$array = [];
$result = [];
foreach($array_data as $key => $values)
{
  $data = explode('>', $values);
  if(is_array($data))
  {
    $count = 0;
    $pop = array_pop($data);
    foreach($data as $k => $v)
    {
        if($count == 1)
        {
            unset( $result[$last_key] );
            $result[$last_key][$v] = $pop;
            $count++;
            continue;
        }
        $last_key = $v;
        $result[$v] = $pop;
        $count++;
    }
    $array[] = $result;
  }
 }

and the result will be something like

echo '<pre>';print_r( $array );
Array
(
  [0] => Array
    (
        [Cars ] =>  Model1
    )

[1] => Array
    (
        [Cars ] => Array
            (
                [ Model1 ] =>  Accessories
            )
    )

 [2] => Array
    (
        [Cars ] => Array
            (
                [ Model1 ] =>  Something
            )

    )

[3] => Array
    (
        [Cars ] =>  Model2
    )

[4] => Array
    (
        [Cars ] =>  Model3
    )

 )

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问