2013-03-29 65 views
-3

我有一個從api調用返回的數組。有時它只會是一組,而另一組則是其他組。下面的例子中,採取頁面「查看源」:PHP通過數組循環並回顯所選項目

[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($響應)我想能夠通過項目環和回聲只是區域和前綴在一個不錯的名單。

我該怎麼做?

非常感謝您的幫助。

+1

http://php.net/foreach –

回答

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

}

+0

謝謝但可悲的是它不工作。我只是得到區域:然後沒有和前綴:然後沒有。樣式是在那裏用線和換行符,但數組項不會被打印。 – user1903663

+0

嘗試更新的代碼:) –

+0

嗨漢克Panky,這個作品: – user1903663