数组

时间:2016-07-05 20:22:06

标签: php arrays facebook laravel foreach

我正在进行facebook图表api调用并获取数据。但由于某种原因无法循环它。是因为它受到保护吗?或者我的foreach循环是错误的。

这是我在视图中使用var_export($ user_pages)时的数据

array (size=2)
  'data' => 
    array (size=2)
      0 => 
        object(stdClass)[92]
          public 'access_token' => string 'EAAZAbF70RRXMBADY1VdKYpEF2gKsM2n6ygicG2ZB94D6wrzBUSYg5Xn3psz7p8pGRpmjaZCP2U9JTrlPsvhm3345CyMIKVTGQk4GOZAS3BxNM4fZAZCKPRQQriFpaTnSfYMDhtnWZCgeZCM4hvt6r49LzaO7CttIy5MZD' (length=166)
          public 'category' => string 'Book Store' (length=10)
          public 'category_list' => 
            array (size=1)
              0 => 
                object(stdClass)[93]
                  public 'id' => string '197048876974331' (length=15)
                  public 'name' => string 'Book Store' (length=10)
          public 'name' => string 'Unofficial Dev777' (length=17)
          public 'id' => string '1791907811022418' (length=16)
          public 'perms' => 
            array (size=6)
              0 => string 'ADMINISTER' (length=10)
              1 => string 'EDIT_PROFILE' (length=12)
              2 => string 'CREATE_CONTENT' (length=14)
              3 => string 'MODERATE_CONTENT' (length=16)
              4 => string 'CREATE_ADS' (length=10)
              5 => string 'BASIC_ADMIN' (length=11)
      1 => 
        object(stdClass)[94]
          public 'access_token' => string 'EAAZAbF70RRXMBAEZAVIJIx5vtvpZAjloLl0yjZBcErnxEzSiqZAQ9wRdo49Be5XXaO98ecQLN0zQVnjpViURgqilZBdfZAV8EuEriW13f0SlZCMoSUjenAp36X6RMeWI1DnxIrclW20vqL6hwgbfm9PZAxDB8lRUOcmkZD' (length=167)
          public 'category' => string 'Business Services' (length=17)
          public 'category_list' => 
            array (size=1)
              0 => 
                object(stdClass)[95]
                  public 'id' => string '187133811318958' (length=15)
                  public 'name' => string 'Business Services' (length=17)
          public 'name' => string 'Self' (length=4)
          public 'id' => string '495868270618000' (length=15)
          public 'perms' => 
            array (size=6)
              0 => string 'ADMINISTER' (length=10)
              1 => string 'EDIT_PROFILE' (length=12)
              2 => string 'CREATE_CONTENT' (length=14)
              3 => string 'MODERATE_CONTENT' (length=16)
              4 => string 'CREATE_ADS' (length=10)
              5 => string 'BASIC_ADMIN' (length=11)
  'paging' => 
    object(stdClass)[96]
      public 'cursors' => 
        object(stdClass)[97]
          public 'before' => string 'MTc5MTkwNzgxMTAyMjQxOAZDZD' (length=26)
          public 'after' => string 'NDk1ODY4MjcwNjE4MDAw' (length=20

var_export($ user_pages)

array ( 'data' => array ( 0 => stdClass::__set_state(array( 'access_token' => 'EAAZAbF70RRXMBAAesxifymoAnCRnrbp7n8t8sUG3gYkImVVZA1CxkUAmqZBA1SUdh7HZCg3EBHrpAcWiIHb2A5Secyhnx70qGW7hyTStE5KX842ZB5mlzO1RZAjkIsaWzN7e5uZCHZAfbQXLQo7We54ZACDjkbQ4TLIwZD', 'category' => 'Book Store', 'category_list' => array ( 0 => stdClass::__set_state(array( 'id' => '197048876974331', 'name' => 'Book Store', )), ), 'name' => 'Unofficial Dev777', 'id' => '1791907811022418', 'perms' => array ( 0 => 'ADMINISTER', 1 => 'EDIT_PROFILE', 2 => 'CREATE_CONTENT', 3 => 'MODERATE_CONTENT', 4 => 'CREATE_ADS', 5 => 'BASIC_ADMIN', ), )), 1 => stdClass::__set_state(array( 'access_token' => 'EAAZAbF70RRXMBACzv5AV8MGVBTh1U4XgCXh7a9gs9R5JAQZBKf7gsdOhe6DKxk1Sy0cD2QtaDoHZBs6O13hWoL3W39M0WgNtpUssWam2PMskfEF6zZBypmLIiyQlj2ZC2Dq5tqTjH2WohusLdwyrODi4d9kzgBH4ZD', 'category' => 'Business Services', 'category_list' => array ( 0 => stdClass::__set_state(array( 'id' => '187133811318958', 'name' => 'Business Services', )), ), 'name' => 'Self', 'id' => '495868270618000', 'perms' => array ( 0 => 'ADMINISTER', 1 => 'EDIT_PROFILE', 2 => 'CREATE_CONTENT', 3 => 'MODERATE_CONTENT', 4 => 'CREATE_ADS', 5 => 'BASIC_ADMIN', ), )), ), 'paging' => stdClass::__set_state(array( 'cursors' => stdClass::__set_state(array( 'before' => 'MTc5MTkwNzgxMTAyMjQxOAZDZD', 'after' => 'NDk1ODY4MjcwNjE4MDAw', )), )), )

这是我的循环,它抛出一个错误说"为foreach()提供的参数无效(查看:/Library/WebServer/Documents/fbLogin/app/views/hello.blade.php) "

@foreach($user_pages as $pages)
                {{ $pages->name }}
                {{ $pages->category }}
@endforeach

我也试过var_dump($ user_pages-> backingData)它显示错误说"无法访问受保护的属性Facebook \ GraphObject :: $ backingData"

是无法访问的数据吗?

已发布var_export的结果 **好吧我在调用graph api时使用了facebook asArray()方法,现在数据更具可读性。只需要循环它。 * 在asArray()

之后使用新的var_dump更新了帖子

1 个答案:

答案 0 :(得分:0)

将我的api请求更改为

return $response->getGraphObject()->getProperty('data')->asArray();

<强>从

return $response->getGraphObject();

现在以简单的数组形式获取数据,通过它我可以使用foreach轻松循环

@foreach($user_pages as $user_pages)
                {{ $user_pages->name }}
                {{ $user_pages->category }}
@endforeach