繁体   English   中英

使用FQL的Facebook PHP SDK Birthday APP

[英]Facebook PHP SDK Birthday APP using FQL

我是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:我希望能够代表div中每个月的生日,并按月的日期排序。 谢谢。

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

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

或者,您可以将日期/月份设置为数组键并根据以下键进行排序:

$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允许您对他们进行排序,因此最好让他们来做。 在某些情况下,查询后排序非常有用,这通常就是我喜欢这样做的方式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM