开发者

Use a foreach loop to access the array within this queryresult object?

A print_r of my object ($results) returns the following:

QueryResult Object 
( [queryLocator] => [done] => 1 [records] => 
Array ( [0] => SObject Object ( [type] => Partner_Research__c [fields] => stdClass Object ( [Partner_Research_URL__c] => http://www.forms.com/184320?tfa_PRID=PR-0167 [Partner_Research_Name__c] => MM Sample Organization-TBR Partner 2011 [Id] => a0V80000003FwjjEAC ) [Id] => a0V80000003FwjjEAC ) 
[1] => SObject Object ( [type] => Partner_Research__c [fields] => stdClass Object ( [Partner_Research_URL__c] => http://www.forms.com/184320?tfa_PRID=PR-0170 [Partner_Research_Name__c] => Kansas City, Missouri Public Schools-TBR Partner 2011 [Id] => a0V80000003Fxf9EAC ) [Id] => a0V80000003Fxf9EAC ) 
[2] => SObject Object ( [type] => Partner_Research__c [fields] => stdClass Object ( [Partner_Research_URL__c] => http://www.forms.com/184320?tfa_PRID=PR-0169 [Partner_Research_Name__c] => Newark Public Schools-TBR Partner 2011 [Id] => a0V80000003FxQ2EAK ) [Id] => a0V80000003FxQ2EAK ) 
[3] => SObject Object ( [type] => Partner_Research__c [fields] => stdClass Object ( [Partner_Research_URL__c] => http://www.forms.com/184320?tfa_PRID=PR-0168 [Partner_Research_Name__c] => Breakthrough Charter Schools-TBR Partner 2011 [Id] => a0V80000003FxPxEAK ) [Id] => a0V80000003FxPxEAK ) 
[4] =>开发者_如何学编程 SObject Object ( [type] => Partner_Research__c [fields] => stdClass Object ( [Partner_Research_URL__c] => http://www.forms.com/184320?tfa_PRID=PR-0004 [Partner_Research_Name__c] => KIPP, San Antonio-TBR Partner 2011 [Id] => a0V80000003FrBUEA0 ) [Id] => a0V80000003FrBUEA0 ) 
[5] => SObject Object ( [type] => Partner_Research__c [fields] => stdClass Object ( [Partner_Research_URL__c] => http://www.forms.com/184320?tfa_PRID=PR-0003 [Partner_Research_Name__c] => KIPP, Chicago - Gary-TBR Partner 2011 [Id] => a0V80000003FrB5EAK ) [Id] => a0V80000003FrB5EAK )  
[6] => SObject Object ( [type] => Partner_Research__c [fields] => stdClass Object ( [Partner_Research_URL__c] => http://www.forms.com/184320?tfa_PRID=PR-0023 [Partner_Research_Name__c] => Harlem Village Academies-TBR Partner 2011 [Id] => a0V80000003FrEOEA0 ) [Id] => a0V80000003FrEOEA0 ) ) [size] => 7 ) 

I want to use a loop similar to the what is shown below to display a series of results however the foreach statement is incorrect.

  foreach ($results as $result)
  {
    $id = $result[fields][Id];
    $name = $result[fields][Partner_Research_Name__c];
    $url = $result[fields][Partner_Research_URL__c];

    $html .= "<tr><td>$id</td><td>$name</td><td>$url</td></tr>";
  }

What changes do I need to make to the foreach statement to get my code back on track?


What helps me in problems like this is to try to print inside the foreach loop. For example, you can do a var_dump of each $result and see what that structure is, and it could help determine how to proceed.


Here is how I eventually did it, thanks for the help provided by contributors.

  foreach ($results->records as $result)
  { 
    $id = $result->fields->Id;
    $name = $result->fields->Partner_Research_Name__c;
    $url = $result->fields->Partner_Research_URL__c;

    $html .= "<tr><td>$id</td><td>$name</td><td>$url</td></tr>";
  }


$results is the name of the QueryResults object, I don't know what the real name in your code is.

foreach ($results->records as $result)
{
  $id = $result->Id;
  $name = $result->fields->Partner_Research_Name__c;
  $url = $result->fields->Partner_Research_URL__c;

  $html .= "<tr><td>$id</td><td>$name</td><td>$url</td></tr>";
}


If I'm reading your sample correctly, your items are std objects instead of arrays. You may need to reference them like this:

  foreach ($results as $result)
  {
    $id = $result->fields->Id;
    $name = $result->fields->Partner_Research_Name__c;
    $url = $result->fields->Partner_Research_URL__c;

    $html .= "<tr><td>$id</td><td>$name</td><td>$url</td></tr>";
  }

Your print_r sample above is difficult to read. If you could provide it with the indentation it would be helpful.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜