如何显示多维数组

发布于 2024-12-11 00:32:16 字数 619 浏览 0 评论 0原文

我有一个与此类似的数组:

$suppliers = array(
  'Utility Warehouse' => array('Gas' => array(0,0), 'Electricty' => array(0,0)),
  'British Gas' => array('Gas' => array(93,124), 'Electricty' => array(93,124)),
  'Eon' => array('Gas' => array(93,124), 'Electricty' => array(93,124))
);

如何显示如下信息?

Utility Warehouse
Gas: 0-0 Electricity 0-0

British Gas
Gas: 93-134 Electricity: 93-134

Eon
Gas: 93-124 Electricity: 93-134

您可以看到显示的数据与数组中的数据如何对应。我已经尝试过:

foreach($suppliers as $a){
    echo $a[0];
}

但这没有任何作用。

I have this array similar to this:

$suppliers = array(
  'Utility Warehouse' => array('Gas' => array(0,0), 'Electricty' => array(0,0)),
  'British Gas' => array('Gas' => array(93,124), 'Electricty' => array(93,124)),
  'Eon' => array('Gas' => array(93,124), 'Electricty' => array(93,124))
);

How can I display the information as follows?

Utility Warehouse
Gas: 0-0 Electricity 0-0

British Gas
Gas: 93-134 Electricity: 93-134

Eon
Gas: 93-124 Electricity: 93-134

You can see how the displayed data corresponds to the data in the array. I've tried this:

foreach($suppliers as $a){
    echo $a[0];
}

But this does nothing.

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

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

发布评论

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

评论(4

放赐 2024-12-18 00:32:17
<?php 

foreach($suppliers as $supplier => $category) {
    echo $supplier . '<br />';
    foreach($category as $cat_name => $values_arr) {
        echo $cat_name . ': ' . implode('-', $values_arr) . '<br /><br />';
    }
}

?>
<?php 

foreach($suppliers as $supplier => $category) {
    echo $supplier . '<br />';
    foreach($category as $cat_name => $values_arr) {
        echo $cat_name . ': ' . implode('-', $values_arr) . '<br /><br />';
    }
}

?>
秋心╮凉 2024-12-18 00:32:17

你可以尝试:

foreach($suppliers as $name => $value) {
    echo $name . "<br />";
    foreach($value as $a => $price) {
        echo $a .': '. $price[0].'-'.$price[1];
    }
    echo "<br /><br />";
}

you can try:

foreach($suppliers as $name => $value) {
    echo $name . "<br />";
    foreach($value as $a => $price) {
        echo $a .': '. $price[0].'-'.$price[1];
    }
    echo "<br /><br />";
}
爱本泡沫多脆弱 2024-12-18 00:32:17

下面是实现您想要的代码,但我建议您多温习一下您的 PHP 技能,因为这是一项微不足道的任务。

foreach($suppliers as $name => $data){
    echo $name . '<br/>';
    foreach($data as $utility => $value){
        echo $utility . ': ' . $value[0] . '-' . $value[1] . ' ';
    }
    echo '<br/><br/>';
}

The code to achieve what you want would be following, but i suggest you brush up on your PHP skills a bit more, as this is a trivial task.

foreach($suppliers as $name => $data){
    echo $name . '<br/>';
    foreach($data as $utility => $value){
        echo $utility . ': ' . $value[0] . '-' . $value[1] . ' ';
    }
    echo '<br/><br/>';
}
暮年 2024-12-18 00:32:17

这是一个可以使用的简单递归函数。我找到了它并出于演示目的对其进行了修改。原始出处在评论里。

function print_a($array, $level=0){
  # source: https://thisinterestsme.com/php-using-recursion-print-values-multidimensional-array/
  foreach($array as $key => $value){
    # If $value is an array.
    if(is_array($value)){
      echo str_repeat("-", $level). "<br>{$key}<br>\r\n";
      # We need to loop through it.
      print_a($value, $level + 1);
    } else{
      # It is not an array, so print it out.
      echo str_repeat("-", $level) . "{$key}: {$value}<br>\r\n";
    }
  }
} # END FUNCTION print_a

Here is a simple recursive function one can use. I found it and modified it for presentation purposes. The original source is in the comments.

function print_a($array, $level=0){
  # source: https://thisinterestsme.com/php-using-recursion-print-values-multidimensional-array/
  foreach($array as $key => $value){
    # If $value is an array.
    if(is_array($value)){
      echo str_repeat("-", $level). "<br>{$key}<br>\r\n";
      # We need to loop through it.
      print_a($value, $level + 1);
    } else{
      # It is not an array, so print it out.
      echo str_repeat("-", $level) . "{$key}: {$value}<br>\r\n";
    }
  }
} # END FUNCTION print_a
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文