在php中将多维数组转换为字符串

时间:2015-11-27 13:10:21

标签: php arrays multidimensional-array

Array ( [0] => LOWER CERVICAL ) 
Array ( [0] => LOWER CERVICAL [1] => Degenerative ) 
Array ( [0] => LOWER CERVICAL [1] => Degenerative [2] => Degenerative )  
Array ( [0] => LOWER CERVICAL [1] => Degenerative [2] => Degenerative [3] => Cervical myelopathy(Spondylotic) )

我正在使用数组来保存结果,但我不知道如何将上面的数组格式转换为下面的字符串格式。我必须将上面的数组格式转换为预期输出中给出的String

Expeceted Output:

 LOWER CERVICAL>>Degenerative>>Degenerative>>Cervical myelopathy(Spondylotic)>>

代码:

while($rsdiag=mysql_fetch_array($sqldiag))
{
    $items[$i] = $rsdiag['di_name'];
    echo $rsdiag['di_name'].">>";
    print_r($items);
    $i++;
}

1 个答案:

答案 0 :(得分:0)

我想注意这可能不是很好但是这里有一些解决方案,只有当你没有大量的数据要进行时我才能建议:

rsort($array);
$map = array();

// $array is your strings array
foreach($array as $item) {
  if( empty($map)){
    // First element catch the express..
    $map[] = $item;
    continue;
  }
  $notFound = TRUE;
  foreach($map as $mapItem){
    if( array_intersect_assoc($item, $mapItem) == $item){
      $notFound = FALSE;
    }
  }
  if( $notFound === TRUE) {
    $map[] = $item;
  }
}
foreach($map as $item){
  echo implode(" >> ",$item);
}