user1007686
user1007686

Reputation:

Number of items in string

I'm trying to get how many times subject occurs in this string from an FQL table. I've tried SUM, Count, explode, etc. and every possible combination thereof that I could think of.

All I ever get is either the first item or an array of errors.

I'm new to PHP and get stuck on the dumbest things but after hours of unsuccessful attempts I turn to the professionals.

try {
    $me = $facebook->api('/me');
    $fql    =   "SELECT subject FROM photo_tag WHERE subject=me()";
    $param  =   array(
                      'method'    => 'fql.query',
                      'query'     => $fql,
                      'callback'  => ''
                      );
    $fqlResult   =   $facebook->api($param);

    foreach($fqlResult as $row){
            //sum, count, explode or somthing? 

        $photo_tag_count = $row['subject'];

    }
        echo "$photo_tag_count";


} catch (FacebookApiException $e) {
    error_log($e);
}

Here's an example of the array:

[{"subject": "123"},{"subject": "123"},{"subject": "123"}]

So I'm looking for 3 as the answer.

Thanks

Upvotes: 0

Views: 197

Answers (2)

Mike Purcell
Mike Purcell

Reputation: 19999

Just re-read OP, kind of confusing, based response on "example of array".

If $fqlResult is a string

echo count(explode(',', $fqlResult));

If $fqlResult is an array

echo count($fqlResult);

Upvotes: 0

ehime
ehime

Reputation: 8425

try $photo_tag_count = count($row['subject']); or it would be try $photo_tag_count = count($row); one of the two should work, I don't know what your array looks like, so its hard to figure out what to count.

Hell, you can even do this.

$photo_tag_count = 0 
 foreach($fqlResult as $row){

        if($row['subject']) {$photo_tag_count++;}

    }
        echo "$photo_tag_count";


} catch (FacebookApiException $e) {
    error_log($e);
}

Upvotes: 1

Related Questions