douren9077 2011-11-23 02:20
浏览 20

从Facebook GRAPH API解析数组

I'm using PHP to handle Facebook GRAPH API calls. I need a better way to parse the output. Here are a few examples of specifically what I mean. Below is some output regarding education. So as the output is now I have to write like fifteen different elseif statements to handle different possibilities. If first education is set and type is college, do this, if first education isn't set, if first education is set & is high school, etc. Is there an easier way to handle this?

 [education] => Array
    (
        [0] => Array
            (
                [school] => Array
                    (
                        [id] => 111894272160018
                        [name] => Spanish River Community High School
                    )

                [year] => Array
                    (
                        [id] => 137616982934053
                        [name] => 2006
                    )

                [type] => High School
            )

        [1] => Array
            (
                [school] => Array
                    (
                        [id] => 35078114590
                        [name] => University of Central Florida
                    )

                [year] => Array
                    (
                        [id] => 118118634930920
                        [name] => 2012
                    )

                [concentration] => Array
                    (
                        [0] => Array
                            (
                                [id] => 104076956295773
                                [name] => Computer Science
                            )

                        [1] => Array
                            (
                                [id] => 107870585903083
                                [name] => Finance
                            )

                    )

                [type] => College
            )

    )

Another example is when accessing user's friends data. It comes back in an array just like the code above. I want to write some code like if a user's friends education type is college and major is this, then do that. But I obviously won't write 215 expressions to handle each outset [0],[1],[2], for each user. How do I handle this? How can I run the expression on all of them at once? Friend data returned below:

 Array
 (
 [0] => Array
    (
        [name] => BLANK
        [education] => Array
            (
                [0] => Array
                    (
                        [school] => Array
                            (
                                [id] => 114760235206446
                                [name] => Abraham Lincoln High School
                            )

                        [type] => High School
                    )

                [1] => Array
                    (
                        [school] => Array
                            (
                                [id] => 103127603061486
                                [name] => Columbia University
                            )

                        [year] => Array
                            (
                                [id] => 140617569303679
                                [name] => 2007
                            )

                        [concentration] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 187442601290749
                                        [name] => Industrial Engineering & Operations Research
                                    )

                            )

                        [type] => College
                    )

            )

        [work] => Array
            (
            )

    )

[1] => Array
    (
        [name] => BLANK
        [education] => 
        [work] => 
    )

[2] => Array
    (
        [name] => BLANK
        [education] => Array
            (
                [0] => Array
                    (
                        [school] => Array
                            (
                                [id] => 108087985890571
                                [name] => St. Andrew's School
                            )

                        [year] => Array
                            (
                                [id] => 138383069535219
                                [name] => 2005
                            )

                        [type] => High School
                    )

                [1] => Array
                    (
                        [school] => Array
                            (
                                [id] => 108087985890571
                                [name] => St. Andrew's School
                            )

                        [year] => Array
                            (
                                [id] => 138383069535219
                                [name] => 2005
                            )

                        [type] => High School
                    )

                [2] => Array
                    (
                        [school] => Array
                            (
                                [id] => 20697868961
                                [name] => Boston University
                            )

                        [concentration] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 108654845832522
                                        [name] => Business Administration
                                    )

                            )

                        [type] => College
                    )

                [3] => Array
                    (
                        [school] => Array
                            (
                                [id] => 20697868961
                                [name] => Boston University
                            )

                        [concentration] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 108654845832522
                                        [name] => Business Administration
                                    )

                            )

                        [type] => College
                    )

                [4] => Array
                    (
                        [school] => Array
                            (
                                [id] => 108289315859633
                                [name] => University of Miami
                            )

                        [year] => Array
                            (
                                [id] => 138879996141011
                                [name] => 2013
                            )

                        [type] => Graduate School
                    )

                [5] => Array
                    (
                        [school] => Array
                            (
                                [id] => 108289315859633
                                [name] => University of Miami
                            )

                        [year] => Array
                            (
                                [id] => 138879996141011
                                [name] => 2013
                            )

                        [type] => Graduate School
                    )

            )

        [work] => Array
            (
            )

An example:

 function majorRecommender () {

            if ($this->majorid =='104076956295773' && $this->grad>='2011' || $this->minor='Computer Science' && $this->grad>='2011') {
                echo "<p>Majoring in Computer Science is a definite plus. You're on the right track!<p>";
            } elseif ($this->majorid='104076956295773' && $this->grad<='2011') {
                echo "<p>You majored in Computer Science in college. This should certainly help you in the job market.</p>";
            } elseif ($this->majorid!='104076956295773' && $this->grad<='2011') {
                echo "<p>Have you considered going back to school?</p>";
            } elseif ($this->majorid!='104076956295773' && $this->grad>='2013') {
                echo "<p>You still have time until you graduate. You should consider changing your major to Computer Science</p>";
            } elseif ($this->majorid!='104076956295773' && $this->grad<='2012') {
                echo "<p>You should consider minoring in CS.</p>";
            } elseif ($this->majorid!='104076956295773' && $this->majors!='Computer Science') {
                echo "<p>You should consider majoring in Computer Science, or at the very least minoring in it.</p>";
            }
    }

And the object being instantiated:

 $collegeid = isset($user_profile['education'][1]['school']['id']) ? $user_profile['education'][1]['school']['id'] : null ;
$college = isset($user_profile['education'][1]['school']['name']) ? $user_profile['education'][1]['school']['name'] : null ;
$majorid = isset($user_profile['education'][1]['concentration'][0]['id']) ? $user_profile['education'][1]['concentration'][0]['id'] : null ;
$major = isset($user_profile['education'][1]['concentration'][0]['name']) ? $user_profile['education'][1]['concentration'][0]['name'] : null ;
$majors = isset($user_profile['education'][0]['concentration'][0]['name']) ? $user_profile['education'][0]['concentration'][0]['name'] : null ;
$minor = isset($user_profile['education'][0]['concentration'][1]['name']) ? $user_profile['education'][0]['concentration'][1]['name'] : null ;
$minors = isset($user_profile['education'][1]['concentration'][1]['name']) ? $user_profile['education'][1]['concentration'][1]['name'] : null ;
$grad = isset($user_profile['education'][1]['year']['name']) ? $user_profile['education'][1]['year']['name'] : null ;
$grads = isset($user_profile['education'][0]['year']['name']) ? $user_profile['education'][0]['year']['name'] : null ;
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度