如何在php中格式化数组


how to format a array in php?

我有一个数组$array,它打印出类似于的东西

Array
(
[Terry] => Array
    (
        [2011-10-26] => Array
            (
                [0] => 69.90
                [1] => 69.90
            )
    )
[Travis] => Array
    (
        [2011-10-26] => Array
            (
                [0] => 199.50
            )
        [2011-10-27] => Array
            (
                [0] => 199.50
            )
    )
)

我正试图将数据放入这样的表中:

NAME      2011-10-26     2011-10-27
Terry     2              0
Travis    1              1

我可以这样计数:

foreach($array as $key => $value){  
     echo $key; // this will give me the names
     foreach($value as $keys => $values){
         echo $keys; //this will give me the dates
         echo count($values); // this will give me the count per date
     }
}

我回来的日期是这样的:2011-10-26 2011-10-26 2011-10-27

我玩这个已经有一段时间了,我没有什么想法了。

有什么帮助吗?

感谢

// build a list of all of the available dates
$dates = array();
foreach($array as $key => $value){
    $dates = array_merge($value, $dates);
}
$dates = array_keys($dates);
// You may want to sort the date columns here somehow
echo "Names't".implode("'t",$dates)."'n";
foreach($array as $key => $value){
    echo $key;
    foreach($dates as $d){
        echo "'t".(array_key_exists($d, $value) ? count($value[$d]) : 0);
    }
    echo "'n";
}
$result = array();
$days   = array();
foreach ($array as $person => &$dates)
    $days = array_merge(array_keys($dates), $days);
$days = array_unique($days);
sort($days); // Sort days.
foreach ($array as $person => &$dates) {
    foreach ($days as &$day) 
        // You can also use array_key_exists instead of '@' to suppress the notice.
        $result[$day][$person] = @count($dates[$day]);
}
var_dump($result);

结果:

array(2) {
  ["2011-10-26"]=>
  array(2) {
    ["Terry"]=>
    int(2)
    ["Travis"]=>
    int(1)
  }
  ["2011-10-27"]=>
  array(2) {
    ["Terry"]=>
    int(0)
    ["Travis"]=>
    int(1)
  }
}