Note that there are some explanatory texts on larger screens.

plurals
  1. POParsing arrays from the Facebook GRAPH API
    primarykey
    data
    text
    <p>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 &amp; is high school, etc. Is there an easier way to handle this? </p> <pre><code> [education] =&gt; Array ( [0] =&gt; Array ( [school] =&gt; Array ( [id] =&gt; 111894272160018 [name] =&gt; Spanish River Community High School ) [year] =&gt; Array ( [id] =&gt; 137616982934053 [name] =&gt; 2006 ) [type] =&gt; High School ) [1] =&gt; Array ( [school] =&gt; Array ( [id] =&gt; 35078114590 [name] =&gt; University of Central Florida ) [year] =&gt; Array ( [id] =&gt; 118118634930920 [name] =&gt; 2012 ) [concentration] =&gt; Array ( [0] =&gt; Array ( [id] =&gt; 104076956295773 [name] =&gt; Computer Science ) [1] =&gt; Array ( [id] =&gt; 107870585903083 [name] =&gt; Finance ) ) [type] =&gt; College ) ) </code></pre> <p>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:</p> <pre><code> Array ( [0] =&gt; Array ( [name] =&gt; BLANK [education] =&gt; Array ( [0] =&gt; Array ( [school] =&gt; Array ( [id] =&gt; 114760235206446 [name] =&gt; Abraham Lincoln High School ) [type] =&gt; High School ) [1] =&gt; Array ( [school] =&gt; Array ( [id] =&gt; 103127603061486 [name] =&gt; Columbia University ) [year] =&gt; Array ( [id] =&gt; 140617569303679 [name] =&gt; 2007 ) [concentration] =&gt; Array ( [0] =&gt; Array ( [id] =&gt; 187442601290749 [name] =&gt; Industrial Engineering &amp; Operations Research ) ) [type] =&gt; College ) ) [work] =&gt; Array ( ) ) [1] =&gt; Array ( [name] =&gt; BLANK [education] =&gt; [work] =&gt; ) [2] =&gt; Array ( [name] =&gt; BLANK [education] =&gt; Array ( [0] =&gt; Array ( [school] =&gt; Array ( [id] =&gt; 108087985890571 [name] =&gt; St. Andrew's School ) [year] =&gt; Array ( [id] =&gt; 138383069535219 [name] =&gt; 2005 ) [type] =&gt; High School ) [1] =&gt; Array ( [school] =&gt; Array ( [id] =&gt; 108087985890571 [name] =&gt; St. Andrew's School ) [year] =&gt; Array ( [id] =&gt; 138383069535219 [name] =&gt; 2005 ) [type] =&gt; High School ) [2] =&gt; Array ( [school] =&gt; Array ( [id] =&gt; 20697868961 [name] =&gt; Boston University ) [concentration] =&gt; Array ( [0] =&gt; Array ( [id] =&gt; 108654845832522 [name] =&gt; Business Administration ) ) [type] =&gt; College ) [3] =&gt; Array ( [school] =&gt; Array ( [id] =&gt; 20697868961 [name] =&gt; Boston University ) [concentration] =&gt; Array ( [0] =&gt; Array ( [id] =&gt; 108654845832522 [name] =&gt; Business Administration ) ) [type] =&gt; College ) [4] =&gt; Array ( [school] =&gt; Array ( [id] =&gt; 108289315859633 [name] =&gt; University of Miami ) [year] =&gt; Array ( [id] =&gt; 138879996141011 [name] =&gt; 2013 ) [type] =&gt; Graduate School ) [5] =&gt; Array ( [school] =&gt; Array ( [id] =&gt; 108289315859633 [name] =&gt; University of Miami ) [year] =&gt; Array ( [id] =&gt; 138879996141011 [name] =&gt; 2013 ) [type] =&gt; Graduate School ) ) [work] =&gt; Array ( ) </code></pre> <p>An example:</p> <pre><code> function majorRecommender () { if ($this-&gt;majorid =='104076956295773' &amp;&amp; $this-&gt;grad&gt;='2011' || $this-&gt;minor='Computer Science' &amp;&amp; $this-&gt;grad&gt;='2011') { echo "&lt;p&gt;Majoring in Computer Science is a definite plus. You're on the right track!&lt;p&gt;"; } elseif ($this-&gt;majorid='104076956295773' &amp;&amp; $this-&gt;grad&lt;='2011') { echo "&lt;p&gt;You majored in Computer Science in college. This should certainly help you in the job market.&lt;/p&gt;"; } elseif ($this-&gt;majorid!='104076956295773' &amp;&amp; $this-&gt;grad&lt;='2011') { echo "&lt;p&gt;Have you considered going back to school?&lt;/p&gt;"; } elseif ($this-&gt;majorid!='104076956295773' &amp;&amp; $this-&gt;grad&gt;='2013') { echo "&lt;p&gt;You still have time until you graduate. You should consider changing your major to Computer Science&lt;/p&gt;"; } elseif ($this-&gt;majorid!='104076956295773' &amp;&amp; $this-&gt;grad&lt;='2012') { echo "&lt;p&gt;You should consider minoring in CS.&lt;/p&gt;"; } elseif ($this-&gt;majorid!='104076956295773' &amp;&amp; $this-&gt;majors!='Computer Science') { echo "&lt;p&gt;You should consider majoring in Computer Science, or at the very least minoring in it.&lt;/p&gt;"; } } </code></pre> <p>And the object being instantiated:</p> <pre><code> $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 ; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload