将数组数据作为对象返回

时间:2014-04-07 21:44:48

标签: php jquery object

我无法从函数中获取所需的输出。我看到当我执行&#var; var dump'时它会返回正确的数据。但是,它要么没有显示任何内容,要么我得到无效的参数错误/'类stdClass的对象无法转换为字符串'。

这是函数

     public function searchUser($name, $limit = 0) {
     return $this->_makeCall('users/search', false, array('q' => $name, 'count' => $limit));
     }

我的代码就像这样调用它:

    $usearch=$_POST['usersearch'];



    $result = $instagram->searchUser($usearch);

...

    <div id="searchresults">

    <h4> Search result for <?php
     echo $usearch;
 ?>
 </h4>


    <section id="list">
    <?php



    foreach ($usearch as $object) {
    print $object->username;
    }


    ?>

    </div>

  </div>

最后,当我这样称呼时,这是var转储:

    object(stdClass)#2 (2) { ["meta"]=> object(stdClass)#3 (1) { ["code"]=> int(200) } ["data"]=> array(50) { [0]=> object(stdClass)#4 (6) { ["username"]=> string(6) "george" ["bio"]=> string(0) "" ["website"]=> string(0) "" ["profile_picture"]=> string(57) "http://images.ak.instagram.com/profiles/anonymousUser.jpg" ["full_name"]=> string(0) "" ["id"]=> string(7) "7693231" } [1]=> object(stdClass)#5 (6) { ["username"]=> string(9) "instagod7" ["bio"]=> string(28) "Graphic designer 🫠O.D.U." ["website"]=> string(0) "" ["profile_picture"]=> string(76) "http://images.ak.instagram.com/profiles/profile_26017769_75sq_1358524943.jpg" ["full_name"]=> string(6) "George" ["id"]=> string(8) "26017769" } [2]=> object(stdClass)#6 (6) { ["username"]=> string(17) "georgetowncupcake" ["bio"]=> string(81) "Official Instagram account of Georgetown Cupcake | Home of TLC's DC CUPCAKES!" ["website"]=> string(32) "http://www.georgetowncupcake.com" ["profile_picture"]=> string(77) "http://images.ak.instagram.com/profiles/profile_265095138_75sq_1391136095.jpg" ["full_name"]=> string(18) "Georgetown Cupcake" ["id"]=> string(9) "265095138" } [3]=> object(stdClass)#7 (6) { ["username"]=> string(11) "georgewbush" ["bio"]=> string(79) "43rd President of the United States and Founder of the George W. Bush Institute" ["website"]=> string(21) "http://bushcenter.org" ["profile_picture"]=> string(77) "http://images.ak.instagram.com/profiles/profile_531790154_75sq_1377896593.jpg" ["full_name"]=> string(14) "George W. Bush" ["id"]=> string(9) "531790154" } [4]=> object(stdClass)#8 (6) { ["username"]=> string(11) "georgelopez" ["bio"]=> string(50) "New Saint George Episode April 10th on @FXNetworks" ["website"]=> string(26) "http://www.georgelopez.com" ["profile_picture"]=> string(76) "http://images.ak.instagram.com/profiles/profile_38811207_75sq_1390655218.jpg" ["full_name"]=> string(12) "George Lopez" ["id"]=> string(8) "38811207" } [5]=> object(stdClass)#9 (6) { ["username"]=> string(14) "georgerauscher" ["bio"]=> string(33) "photography | munich - Impressum:" ["website"]=> string(31) "http://www.george.li/impressum/" ["profile_picture"]=> string(77) "http://images.ak.instagram.com/profiles/profile_237349510_75sq_1391160241.jpg" ["full_name"]=> string(18) "George A. Rauscher" ["id"]=> string(9) "237349510" }

1 个答案:

答案 0 :(得分:3)

假定vardump是$ result变量,你要迭代的对象数组包含在data属性中:

foreach ($result->data as $object) {
    print $object->username;
}