如何回应多维数组?

时间:2012-11-05 06:43:01

标签: php codeigniter

我有字符串数据

$data="100,200,300,400|500,600,700,800|900,100,200,300|400,500,600,700|800,900,100,200|300,400,500,600|700,800,900,100";

我希望得到这样的结果

a   |b   |c   |d   |
100 |200 |300 |400 |
500 |600 |700 |800 |
900 |100 |200 |300 |
.   |.   |.   |.   |
etc |etc |etc |etc |

我必须做些什么才能得到像这样的结果

我是编程新手,并不知道多维数组

所有我知道我爆炸了两次

$data = explode(';',$data);
foreach($data as &$d)
    $d=explode(':',$d);

5 个答案:

答案 0 :(得分:4)

以这种方式试试

<?php
$data="100,200,300,400|500,600,700,800|900,100,200,300|400,500,600,700|800,900,100,200|300,400,500,600|700,800,900,100";
$data = explode('|',$data);
foreach($data as $d){
   $newdata_arr = explode(',',$d);
      foreach($newdata_arr as $newdata)
      echo $newdata."|";

   echo "\r\n";
}
?>

demo

答案 1 :(得分:2)

您可以将第二个参数更改为str_pad,以更改行中每个元素的填充量。

<?php
$data="100,200,300,400|500,600,700,800|900,100,200,300|400,500,600,700|800,900,100,200|300,400,500,600|700,800,900,100";

$rows = explode("|", $data);
$headers = "a,b,c,d";
array_unshift($rows, $headers);

foreach ($rows as $row) {
  $parts = explode(",", $row);
  foreach ($parts as $part) {
    echo str_pad($part, 10) . "|";
  }
  echo "\n";
}

<强>输出

a         |b         |c         |d         |
100       |200       |300       |400       |
500       |600       |700       |800       |
900       |100       |200       |300       |
400       |500       |600       |700       |
800       |900       |100       |200       |
300       |400       |500       |600       |
700       |800       |900       |100       |

如果您要获得不同的数据,但总是希望值和列分隔符之间至少有1个空格,那么您可以使用以下内容:

<?php
$data="100,200,30,400|500,600,70,8000|900,100,200,300|4004532,500,60,700|800,900,100,200|300,400,500,600|700,800,900,100";

function splitvals($str) {
  return explode(",", $str);
}

$multi_data = array_map('splitvals', explode('|', $data));
array_unshift($multi_data, null);
$val_for_sizes = call_user_func_array('array_map', $multi_data);
$sizes = array();
foreach ($val_for_sizes as $vals) {
  $sizes[] = strlen(max($vals));
}


$rows = explode("|", $data);
$headers = "a,b,c,d";
array_unshift($rows, $headers);

foreach ($rows as $row) {
  $parts = explode(",", $row);
  $col = 0;
  foreach ($parts as $part) {
    echo str_pad($part, $sizes[$col++] + 1) . "|";
  }
  echo "\n";
}

<强>输出

a       |b   |c   |d    |
100     |200 |30  |400  |
500     |600 |70  |8000 |
900     |100 |200 |300  |
4004532 |500 |60  |700  |
800     |900 |100 |200  |
300     |400 |500 |600  |
700     |800 |900 |100  |

答案 2 :(得分:1)

$data = explode('|',$data);
$arr = array();
foreach($data as &$d)
    $arr[] = explode(',',$d);


<pre>
<?php print_r ($arr); ?>
</pre>

答案 3 :(得分:0)

试试这个

$data="100,200,300,400|500,600,700,800|900,100,200,300|400,500,600,700|800,900,100,200|300,400,500,600|700,800,900,100";
$dt = $data = explode('|',$data);
echo 'a | b | c | d';
echo '<br/>';
foreach($dt as $key => $val){
  echo str_replace(',', ' | ',$val);
  echo '<br/>';
 }

答案 4 :(得分:-2)

<pre>
<?php print_r ($array); ?>
</pre>