通过比较键/值对将简单数组转换为多维数据 [英] Converting simple array to multidimensional by comparing key/value pair

查看:65
本文介绍了通过比较键/值对将简单数组转换为多维数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个数组:

I am having this array:

array (size=4)
  0 => 
    array (size=3)
      'name' => string '1' (length=1)
      'parent' => string '1' (length=1)
      'child' => string '1' (length=1)
  1 => 
    array (size=3)
      'name' => string '22' (length=2)
      'parent' => string '1' (length=1)
      'child' => string '22' (length=2)
  2 => 
    array (size=3)
      'name' => string '23' (length=2)
      'parent' => string '1' (length=1)
      'child' => string '23' (length=2)
  3 => 
    array (size=3)
      'name' => string '30' (length=2)
      'parent' => string '23' (length=2)
      'child' => string '30' (length=2)



我想以这样的方式生成一个多级数组:

数组0将是我的根。

其他数组的父值等于name数组0的值将在数组下形成一个树。

例如

数组1和数组2的父= 1等于数组0的名称,这意味着它们将形成一个直接节点到数组0

但是数组3将形成一个到arrar 2的节点,因为它的父值等于数组2的名称值。

和那样。

请帮帮我。我已经尝试了几个递归函数,但所有努力都证明是无效的。


I want to generate a multi level array in such a way that:
array 0 will be my root.
every other array that has their parent value equals to name value of array 0 will form a tree under the array.
e.g
array 1 and array 2 has parent=1 which is equal to name of array 0, which means they will form a direct node to array 0
but array 3 will form a node to arrar 2 because it has its parent value to equal array 2 name value.
And like that.
Please help me. I have tried several recursive functions but all effort prove abortive.

推荐答案

是的。感谢房子。

我通过关注这个博客解决了这个问题 http://leon.vankammen.eu/tech/storing-retrieving-hierarchical-trees-between-php-and-mysql.html [ ^ ]
Yep. Thanks to the house.
I got it solved by following this blog http://leon.vankammen.eu/tech/storing-retrieving-hierarchical-trees-between-php-and-mysql.html[^]


这篇关于通过比较键/值对将简单数组转换为多维数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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