如何过滤API数组对象响应

时间:2016-07-08 03:51:25

标签: php arrays regex cakephp

                $storiesResponse = $login->getStoriesResponse();
            if (strpos($storiesResponse, 'mzaboss') == false) {
                echo "mzaboss is not available! die! \n";
                die;
            } else {
        foreach($storiesResponse->getFriendStories() as $friendStories){
            $friendStoriesUsername = $friendStories->getUsername();
                $storiesContainer = $friendStories->getStories();    
        foreach($storiesContainer as $storyContainer){
            $story = $storyContainer->getStory();
            echo "Mark as viewed story ID: " . $story->getId() . "\n";
                $snapchat->markStoryViewed($story->getId());
            }
            $account_id = $i + 1;
            // return true;
            }
        }
    }

API响应:

object(X\API\Response\Model\FriendStories)#14161 (2) {
  ["username":"X\API\Response\Model\FriendStories":private]=>
  string(7) "mzaboss"
  ["stories":"X\API\Response\Model\FriendStories":private]=>
  array(1) {
    [0]=>
    object(X\API\Response\Model\FriendStoryContainer)#14166 (2) {
      ["story":"X\API\Response\Model\FriendStoryContainer":private]=>
      object(X\API\Response\Model\Story)#14168 (16) {
        ["id":"X\API\Response\Model\Story":private]=>
        string(21) "mzaboss~1467880186851"
        ["username":"X\API\Response\Model\Story":private]=>
        string(7) "mzaboss"
        ["mature_content":"X\API\Response\Model\Story":private]=>
        bool(false)
        ["client_id":"X\API\Response\Model\Story":private]=>
        string(44) "MZABOSS~E02D10B1-9D0B-4698-A9E8-EB43F4D4281D"
        ["timestamp":"X\API\Response\Model\Story":private]=>
        int(1467880186851)
        ["media_id":"X\API\Response\Model\Story":private]=>
        string(16) "6315314297815040"
        ["media_key":"X\API\Response\Model\Story":private]=>
        string(44) "Z0xvmwDCcY3leDm3DC4QPdOGh+9va4iYEbb0Zs0YkUQ="
        ["media_iv":"X\API\Response\Model\Story":private]=>
        string(24) "C+omyS1YQncoraMO0iY4jg=="
        ["thumbnail_iv":"X\API\Response\Model\Story":private]=>
        string(24) "WB4E4e0w0ToXZOqBI+z87w=="
        ["media_type":"X\API\Response\Model\Story":private]=>
        int(0)
        ["time":"X\API\Response\Model\Story":private]=>
        float(3)
        ["caption_text_display":"X\API\Response\Model\Story":private]=>
        NULL
        ["zipped":"X\API\Response\Model\Story":private]=>
        bool(false)
        ["time_left":"X\API\Response\Model\Story":private]=>
        int(86296613)
        ["needs_auth":"X\API\Response\Model\Story":private]=>
        bool(false)
        ["is_shared":"X\API\Response\Model\Story":private]=>
        bool(false)
      }
      ["viewed":"X\API\Response\Model\FriendStoryContainer":private]=>
      bool(false)
    }
  }
}
$ storiesResponse是非常长的数组对象。我想找到“mzaboss”是否在该数组对象响应中,如果它存在那么好,如果它不存在,那么就死了。

但总是犯错:

PHP Warning:  strpos() expects parameter 1 to be string, object given in /home/mzapc/test/asstest/adder/marker/marker.php on line 143

mzaboss不可用!死!

mzaboss是否存在。它会死!

任何帮助?,谢谢。

2 个答案:

答案 0 :(得分:0)

$storiesResponsearray而不是string。因此,除了in_array()之外,您应该使用strpos()

strpos()函数查找第一次出现的字符串在另一个字符串中的位置。

并且,in_array()检查数组中是否存在值。

  $storiesResponse = $login->getStoriesResponse();
        if (in_array('mzaboss', $storiesResponse)) {
             foreach($storiesResponse->getFriendStories() as $friendStories){
        $friendStoriesUsername = $friendStories->getUsername();
            $storiesContainer = $friendStories->getStories();    
    foreach($storiesContainer as $storyContainer){
        $story = $storyContainer->getStory();
        echo "Mark as viewed story ID: " . $story->getId() . "\n";
            $snapchat->markStoryViewed($story->getId());
        }
        $account_id = $i + 1;
        // return true;
        }
        } 
        else {
             echo "mzaboss is not available! die! \n";
            die;
     }

答案 1 :(得分:-1)

                    $storiesResponse = $login->getStoriesResponse();
            foreach($storiesResponse->getFriendStories() as $friendStories){
                if($friendStories->getUsername() == "mzaboss"){ 
                    echo "debugger - mzaboss found\n";
                $friendStoriesUsername = $friendStories->getUsername();
                    $storiesContainer = $friendStories->getStories();    
            foreach($storiesContainer as $storyContainer){
                $story = $storyContainer->getStory();
                echo "Mark as viewed story ID: " . $story->getId() . "\n";
                    $snapchat->markStoryViewed($story->getId());
                }
                $account_id = $i + 1;
                // return true;
                }

这会起作用。

但如果" mzaboss"我想要它。不存在我想暂停代码5分钟,然后重新登录第一个帐户。