php中如何格式化数组?

发布于 2024-12-12 11:37:16 字数 1033 浏览 2 评论 0原文

我有一个数组 $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
     }
}

我得到的日期是这样的:< code>2011-10-26 2011-10-26 2011-10-27

我已经玩了一段时间了,但我已经没有想法了。

有什么帮助吗?

谢谢

i have an array $array that prints out something like:

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
            )

    )
)

i am trying to place data into a table like this:

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

i can get the count like this:

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
     }
}

the dates that i get back are like this: 2011-10-26 2011-10-26 2011-10-27

I've been playing with this for a while and i run out of ideas.

any help?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安静被遗忘 2024-12-19 11:37:17
// 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";
}
// 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";
}
深海少女心 2024-12-19 11:37:17
$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)
  }
}
$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);

The 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)
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文