1
$this->load->library('opencloud');
$opencloud = new Opencloud;
$containers = $this->opencloud->list_containers();
print_r($containers);

上面的代码输出以下数组:

数组 ( [0] => 数组 ( [name] => .CDN_ACCESS_LOGS [count] => 2 [bytes] => 606 ) [1] => Array ( [name] => Michael Grigsby [count] => 9 [ bytes] => 891976 ) [2] => Array ( [name] => Random Photos [count] => 0 [bytes] => 0 ) [3] => Array ( [name] => hello [count] = > 10 [bytes] => 1129257 ) [4] => Array ( [name] => hello_world [count] => 1 [bytes] => 659737 ) [5] => Array ( [name] => hi [count ] => 0 [字节] => 0 ) )

当我回显时,$containers[1]['name']我得到:Michael Grigsby。我的问题是如何让脚本输出所有name值而不是简单的一个?

4

3 回答 3

3

您可以在 PHP 的这种经典模式下使用 foreach 循环回显每个数组的名称,如下所示:

foreach($containers as $container){
  echo($container['name']);
}
于 2013-07-18T04:48:35.067 回答
1
<?php

foreach($mainarray as $onearray){

        echo $onearray['name'];

}

?>
于 2013-07-18T04:48:18.783 回答
0

for您可以通过以下方式循环使用循环数组:

for ($i = 0; $i < count($containers); $i++) {
    echo $containers[$i]['name'];
}

或者,您可以使用foreach循环直接获取内容,而无需对数组索引进行任何操作。

于 2013-07-18T04:52:53.790 回答