使用树路径键将关联多维数组转换为一维数组

时间:2016-01-28 15:27:57

标签: php arrays recursion flatten

我有一个像这样的多维关联数组:

array (size=2)
  'a' => 
    array (size=3)
      'b' => string 'this' (length=4)
      'c' => string 'is' (length=2)
      'd' => string 'a test' (length=6)
  'c' => 
    array (size=4)
      0 => int 1
      1 => int 2
      2 => int 3
      3 => 
        array (size=1)
          'd' => string '123' (length=3)

我尝试将其展平为一维关联数组,并将树路径作为键:

array (size=7)
  'a>b' => string 'this' (length=4)
  'a>c' => string 'is' (length=2)
  'a>d' => string 'a test' (length=6)
  'c>0' => string '1' (length=1)
  'c>1' => string '2' (length=1)
  'c>2' => string '3' (length=1)
  'c>3>d' => string '123' (length=3)

在此处查看我的尝试:http://codepad.org/MGaBPpZP

0 个答案:

没有答案