如何将数组转换为另一种格式

时间:2017-01-25 07:20:16

标签: php arrays recursion

这是阵列。

$data = array(
"id"=>1,
"pid"=>0,
"name"=>'first',
"children"=>array(array(
  "id"=>2,
  "pid"=>1,
  "name"=>'1-1'
),array(
  "id"=>3,
  "pid"=>1,
  "name"=>'1-2',
  "childen" =>array(
    "id" => 4,
    "pid" =>3,
    "name" =>'1-3-4'
  )
)

) );

我希望数组的格式如下所示。

first
--1-1
--1-2
----1-3-4

我写了这样的代码,但它不起作用。我不知道为什么。

function getTreeAsList($array, $result=array(), $space=0)
{
$space += 2;
foreach ($array as $k=>$v) {
    if (is_array($v)) {
        $result[] = str_repeat("--", $space)."get";
        getTreeAsList($v, $result, $space);
    }
}
return $result;
}
getTreeList($data);
请告诉我怎么样。感谢。

加入: @Jerry我在你写的时候这样做,但它有效,但我脑子里还有另一个问题,我怎么能得到像这种格式的结果数组。

array(
  array(
    "id"=>1,
    "name"=>"first"
  ),
  array(
    "id"=>2,
    "name"=>"--1-1"
  ),
  array(
    "id"=>3,
    "name"=>"--1-2"
  ),
  array(
    "id"=>4,
    "name"=>"----1-3-4"
  )
)

5 个答案:

答案 0 :(得分:1)

试试这个:)

$iterator = new RecursiveArrayIterator($data); 

iterator_apply($iterator, 'getTreeAsList', array($iterator)); 

function getTreeAsList($iterator, $space=0) { 

  while ( $iterator -> valid() ) { 
      if($iterator->key() === "name"){
        echo str_repeat('--', $space++) .$iterator["name"]."\n";
      }
      if ( $iterator -> hasChildren() ) { 
          getTreeAsList($iterator -> getChildren(), $space);   
      } 
      $iterator -> next(); 
  } 
}

答案 1 :(得分:0)

我明白了! ;)

<?php

$data = array(
"id"=>1,
"pid"=>0,
"name"=>'first',
"children"=>array(array(
  "id"=>2,
  "pid"=>1,
  "name"=>'1-1'
),array(
  "id"=>3,
  "pid"=>1,
  "name"=>'1-2',
  "childen" =>array(
    "id" => 4,
    "pid" =>3,
    "name" =>'1-3-4'
  )
)));

$result = parse($data);

print $result;

function parse($arr, $space = -1) {
    $result = "";
    foreach ($arr as $key => $value) {
        if (is_array($value)) {
            $result.= parse($value, $space + 1);
        } else if ($key == "name") {
            $result.= ($space > 0 ? str_repeat("--", $space) : "") . $value . "\r\n";
        }
    }
    return $result;
}

?>

编辑(结果):

first
--1-1
--1-2
----1-3-4

答案 2 :(得分:0)

<?php

$data = array(
"id"=>1,
"pid"=>0,
"name"=>'first',
"children"=>array(array(
  "id"=>2,
  "pid"=>1,
  "name"=>'1-1'
),array(
  "id"=>3,
  "pid"=>1,
  "name"=>'1-2',
  "children" =>array(
    "id" => 4,
    "pid" =>3,
    "name" =>'1-3-4'
  )
))
);


function getName($data)
{
    if (is_array($data)) {
        echo  "--".$data["name"]."\n";
        if (isset($data["children"])) {
            if (isset($data["children"][0])) {
                foreach ($data["children"] as $child) {
                    getName($child);
                }
            } else {
                getName($data["children"]);
            }
        }
    }
}
getName($data);

我没有非常认真地对待破折号...这只是为了获得递归名称

输出

--first
--1-1
--1-2
--1-3-4
[Finished in 0.0s]

答案 3 :(得分:0)

您可以使用它来获取数组

格式的值
<?php 
$data = array(
"id"=>1,
"pid"=>0,
"name"=>'first',
"children"=>array(array(
  "id"=>2,
  "pid"=>1,
  "name"=>'1-1'
),array(
  "id"=>3,
  "pid"=>1,
  "name"=>'1-2',
  "childen" =>array(
    "id" => 4,
    "pid" =>3,
    "name" =>'1-3-4'
  )
)

) );

$glresult=array();
function getTreeAsList($array, $space='')
{
 global $glresult;

 foreach ($array as $k=>$v) 
 {
  if (is_array($v)) 
  {
   getTreeAsList($v, $space);
  }
  else if ($k =='name')
  {
   $glresult[] = $space.$v;
   $space .= '--';
  }
 }
return $glresult;
}

$result = getTreeAsList($data);

echo '<pre>'; print_r($result);

?>

答案 4 :(得分:-2)

请尝试以下代码。我希望它与您的要求完全相同。

foreach ($data as $key => $value) 
  {
        if($key == 'name')
        {
            echo $value;    
        }
        if(is_array($value))
        {
            foreach ($value as $ckey => $cvalue) 
            {
                foreach ($cvalue as $ck => $cv) 
                {
                    if($ck == 'name')
                    {
                        echo '</br>';
                        echo '--'.$cv;  
                    }
                    if(is_array($cv))
                    {
                        foreach ($cv as $ckk => $cvv) 
                        {
                            if($ckk == 'name')
                            {
                                echo '</br>';
                                echo '----'.$cvv;
                            }
                        }
                    }
                }
            }
        }
  }

输出是:

first
--1-1
--1-2
----1-3-4