2

我有字符串数据

$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);
4

5 回答 5

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";
}
?>

演示

于 2012-11-05T06:48:37.310 回答
2

You can change the second argument to str_pad to change how much padding each element in the row has.

<?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";
}

OUTPUT

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       |

If you are going to have varying data, but always want to have at least 1 space between the values and the column seperator, then you can use the following:

<?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";
}

OUTPUT

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  |
于 2012-11-05T06:51:38.067 回答
1
$data = explode('|',$data);
$arr = array();
foreach($data as &$d)
    $arr[] = explode(',',$d);


<pre>
<?php print_r ($arr); ?>
</pre>
于 2012-11-05T06:46:46.227 回答
0

try this

$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/>';
 }
于 2012-11-05T06:51:16.193 回答
-2
<pre>
<?php print_r ($array); ?>
</pre>
于 2012-11-05T06:44:44.503 回答