使用FQL的Facebook PHP SDK生日应用

发布于 2024-10-29 13:13:04 字数 1156 浏览 0 评论 0原文

我是PHP编程的新手。我使用 FQL 查询来获取所有用户朋友的生日。 但我想根据月份和日期对用户进行排序。在多维阵列中可以做到这一点吗?我想要:

$year = array('jan' => array('1' => array(data returned by fql query), '2' => array(data returned by fql query),..etc), 'feb' =>  array('1' => array(data returned by fql query)), 'mar' => array(), etc);

有可能吗? 到目前为止,我的代码:

$query = "SELECT uid, first_name, last_name, birthday_date, sex, pic_square, current_location, hometown_location FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) AND strlen(birthday_date) != 0 ORDER BY birthday_date";

$year =array();
$prm  =   array(
          'method'    => 'fql.query',       
          'query'     => $query,
          'callback'  => ''     
          );



$friends  =  $facebook->api($prm);

for($m = 1;$m <= 12;$m++){
    $dm = mktime(0,0,0,$m,26);
    $dm = date("M", $dm);
    $year[] = $dm;

    for($d = 1;$d <= 31;$d++){
        $a2 = array($d => array());
        $a3 = $out[$dm];
        array_push($out,array($dm => array($d => "")));
    }                   
}

PS:我希望能够在每月的每月以每月的方式代表生日。 谢谢。

I'm a newbie in PHP programming. I use a FQL query to get birthdays of all user's friends.
But I want to sort the users according to month and day. Can this be possible in multidimensional arrays? I want:

$year = array('jan' => array('1' => array(data returned by fql query), '2' => array(data returned by fql query),..etc), 'feb' =>  array('1' => array(data returned by fql query)), 'mar' => array(), etc);

Is it possible?
My code so far:

$query = "SELECT uid, first_name, last_name, birthday_date, sex, pic_square, current_location, hometown_location FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) AND strlen(birthday_date) != 0 ORDER BY birthday_date";

$year =array();
$prm  =   array(
          'method'    => 'fql.query',       
          'query'     => $query,
          'callback'  => ''     
          );



$friends  =  $facebook->api($prm);

for($m = 1;$m <= 12;$m++){
    $dm = mktime(0,0,0,$m,26);
    $dm = date("M", $dm);
    $year[] = $dm;

    for($d = 1;$d <= 31;$d++){
        $a2 = array($d => array());
        $a3 = $out[$dm];
        array_push($out,array($dm => array($d => "")));
    }                   
}

PS: I want to be able to represent birthdays in each month in a div, order by day of month.
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

划一舟意中人 2024-11-05 13:13:04

multi_sort($myArray[index], SORT_NUMERIC, SORT_ASC) 就是您正在寻找的,并且您可以摆脱嵌套的 for 循环。传递要排序的数组和索引,按数字排序,并根据您的需要以 SORT_ASC 或 SORT_DESC 形式排序。您可能需要先从日期中删除破折号或斜杠,然后进行实验并找出答案。

这是 PHP 手册页。 http://php.net/manual/en/function.array-multisort.php

multi_sort($myArray[index], SORT_NUMERIC, SORT_ASC) is what you are looking for, and you can get rid of the nested for loops. pass the array and index to be sorted by, sort numerically, and in SORT_ASC, or SORT_DESC depending on what you want. You may need to removed dashes or slashes from the dates first, experiment and find out.

Here is the PHP man page. http://php.net/manual/en/function.array-multisort.php

爱的那么颓废 2024-11-05 13:13:04

或者,您可以将日/月设置为数组键并基于排序:

$sort = array();
foreach($friends as $friend)
{
    // note the extra 0's here, to give us padding to filter out duplicate array keys
    // this gives us an int in the format 030100, 093100, etc
    $key = (int)date('Md00', strtotime($friend['birthday_date']));

    // make sure we don't have duplicates...if the key already exists, incrememnt it by one
    while(isset($sort[$key])) $key++;

    $sort[$key] = $friend;
}

// sort the items by array key
ksort($sort);

// $sort now contains your friends, sorted by birthday.

请注意,如上所述,facebook 允许您在其端进行排序,因此最好让他们完成这项工作。在某些情况下,查询后排序非常有用,这通常是我喜欢的方式。

Or you could make the day/month an array key and sort based one that:

$sort = array();
foreach($friends as $friend)
{
    // note the extra 0's here, to give us padding to filter out duplicate array keys
    // this gives us an int in the format 030100, 093100, etc
    $key = (int)date('Md00', strtotime($friend['birthday_date']));

    // make sure we don't have duplicates...if the key already exists, incrememnt it by one
    while(isset($sort[$key])) $key++;

    $sort[$key] = $friend;
}

// sort the items by array key
ksort($sort);

// $sort now contains your friends, sorted by birthday.

Note that as mentioned, facebook allows you do do the sorting on their end, so it's best to let them do the work. There are some cases where post-query sorting is very useful, and this is generally how I like to do it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文