2010-06-25 74 views
0

我正在編寫一個與公司SOAP API接口的php應用程序。我正在對一個API函數進行調用並接收一個對象。然而,當我嘗試使用foreach上的對象變量之一,我收到錯誤:PHP對象具有值,但是當我嘗試在FOREACH()中使用它時我得到NULL值

Warning: Invalid argument supplied for foreach() in ...

我的代碼:

// Make Call and get result 
$result = $soapClient->GetProgramSemesterCategoryList(array("semesterGuid" => "8725a32b-e671-4386-ae70-969a6d600f31")); 

// Output the result 
print("<b>Result = "); 
print_r($result); 
print("</b></br>"); 

// Make sure not getting an exception 
if($result->GetProgramSemesterCategoryListResult->IsException) 
{ 
    print("Error!"); 
} 
else 
{ 
    // Output the type of the CategoryInfo in the object 
    print("<b>TYPE: " . gettype($result->GetProgramSemesterCategoryList->Value->CategoryInfo) . "</b></br>"); 
    foreach($result->GetProgramSemesterCategoryList->Value->CategoryInfo as $category) 
    { 
     print("<b>Categories for this Semester</b></br>"); 

     print("<b>CategoryId:</b> " . $category->CategoryId . " - <b>CategoryGuid:</b> " 
      . $category->CategoryName . " - <b>CategoryName:</b> " . $category->CategoryDesc 
      . " - <b>ModuleFor:</b> " . $category->ModuleFor . "</br></br>"); 
    } 
} 

這裏是正在打印返回的對象:

stdClass Object ( 
    [GetProgramSemesterCategoryListResult] => stdClass Object ( 
     [Value] => stdClass Object ( 
      [CategoryInfo] => Array ( 
       [0] => stdClass Object ( 
        [CategoryId] => 9 
        [CategoryGuid] => 7299eb49-d75a-4719-ae18-8ce814e99931 
        [CategoryName] => Adult Swim Lessons 
        [CategoryDesc] => New Category 
        [ModuleFor] => PRG_Program_Registration 
       ) 
       [1] => stdClass Object ( 
        [CategoryId] => 10 
        [CategoryGuid] => 4117bbaf-7301-40b2-a149-76daca62b748 
        [CategoryName] => Child Swim Lessons 
        [CategoryDesc] => New Category 
        [ModuleFor] => PRG_Program_Registration 
       ) 
      ) 
     ) 
     [IsException] => 
    ) 
) 

下面是對GetType來電顯示:

TYPE = NULL 
+0

Geez。謝謝。 – 2010-06-25 17:00:30

回答

3

您正在使用不同的對象名稱。在foreach中你有GetProgramSemesterCategoryList,但對象有GetProgramSemesterCategoryListResult。最後注意「結果」。

相關問題