English 中文(简体)
PHP 展示多层面阵列
原标题:PHP displaying multidimensional array
  • 时间:2011-10-18 14:12:40
  •  标签:
  • php
  • arrays

我有这样的阵列:

$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];
}

But this does nothing. CONFUSED!

最佳回答
<?php 

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

?>
问题回答

你可以尝试:

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

实现你们想要的东西的法典,但我建议你比照你的购买力平价技能,因为这是一项微不足道的任务。

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

回答是每一个人(我太慢)。 http://codepad.org/PDPEjAGJ“rel=“nofollow”http://codepad.org/PDPEjAGJ

Also, everyone who answered this question, me included, is guilty of spoonfeeding. Ahh well, the things I ll do for points! :p

这里可以使用简单的休养功能。 我发现并作了修改,以供参考。 原始来源在评论中。

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>
";
      # 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>
";
    }
  }
} # END FUNCTION print_a




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签