-3

我有一个从 api 调用返回的数组。有时它只是一组,而在其他情况下会是很多组。示例如下,用于页面“查看源代码”:

[objects] => Array
                (
                    [0] => Array
                        (
                            [stock] => 388
                            [voice_enabled] => 1
                            [region] => BASEL, SWITZERLAND
                            [voice_rate] => 0.00800
                            [prefix] => 61
                            [sms_rate] => -1
                            [number_type] => local
                            [setup_rate] => 0.80000
                            [rental_rate] => 0.80000
                            [group_id] => 25728319905588
                            [sms_enabled] => 
                            [resource_uri] => /v1/Account/MAMTE4MTHJNJRKODBIMD/AvailableNumberGroup/25728319905588/
                        )

                    [1] => Array
                        (
                            [stock] => 593
                            [voice_enabled] => 1
                            [region] => BELLINZONA, SWITZERLAND
                            [voice_rate] => 0.00800
                            [prefix] => 91
                            [sms_rate] => -1
                            [number_type] => local
                            [setup_rate] => 0.80000
                            [rental_rate] => 0.80000
                            [group_id] => 25732565267448
                            [sms_enabled] => 
                            [resource_uri] => /v1/Account/MAMTE4MTHJNJRKODBIMD/AvailableNumberGroup/25732565267448/
                    )

还有10个这样的块。

这是由来自以下 PHP 文件的 api 调用生成的:

<?php
 include("connect.php");
 include("settings.php");
 require_once 'myapiprovider.php';
 $p = new RestAPI($auth_id, $auth_token);
 $params = array( 'country_iso' => 'CH' );
 $response = $p->get_number_group($params);
 print_r($response);
 ?>

而不是 print_r($response) 我希望能够遍历项目并在一个不错的列表中仅回显区域和前缀。

我怎样才能做到这一点?

非常感谢您的帮助。

4

1 回答 1

1
foreach($response["objects"] as $i=>$v)
{
echo "Region: ".$v["region"];
echo "<br>";
echo "Prefix: ".$v["prefix"];   
echo "<hr>";

}

于 2013-03-29T12:22:31.530 回答