2014-01-17 33 views
-5
foreach($top_9_friends as $key => $user) 
{ 

array_push($scores, array( 

    "id" => key($friends_rank_data), 
    "likes" => $user['likes'], 
    "comments" => $user['comments'], 
    "posts" => $user['posts'], 
    if(1 == 1) { 
     $url = 'https://graph.facebook.com/' . key($friends_rank_data) . '/?fields=gender&access_token=' . $access_token ; 
     $contents = file_get_contents($url); 
if($contents) 
{ 
    $dataa = json_decode($contents, true); 
} 
    "sex" => $dataa['data']['0']['gender'],  
    } 
) 

); 
} 

誰能幫我怎麼寫正確的代碼, 我哪裏錯了 如何輸入正確的日期如何創建,如果在陣列

+1

這段代碼**應該做什麼**?你得到的輸出是什麼? – Mureinik

+2

爲什麼要在你的數組中聲明if語句?大聲笑... – Ryan

回答

1

不知道你試圖嘗試什麼,但只是分開你如何構建陣列。

也有沒有需要如果(1 == 1){...},這只是意味着你總是這樣做,爲什麼要麻煩?

foreach ($top_9_friends as $key => $user) { 

    // create array 
    $arr = array(
     "id" => key($friends_rank_data), 
     "likes" => $user['likes'], 
     "comments" => $user['comments'], 
     "posts" => $user['posts'] 
    ); 

    $url = 'https://graph.facebook.com/' . key($friends_rank_data) . '/?fields=gender&access_token=' . $access_token; 
    $contents = file_get_contents($url); 
    if ($contents) { 
     $dataa = json_decode($contents, true); 

     // add this array key inside the if statement 
     $arr["sex"] = $dataa['data']['0']['gender']; 
    } 
    array_push($scores, $arr); 
} 
+0

陣列 ( [0] =>數組 ( [ID] => 100000382928634 [喜歡] => 10 [註釋] => 6 [帖] => 41 [性別] => ) ) – user3186365

1

執行,如果前陣:

foreach($top_9_friends as $key => $user) 
{ 

    if(1 == 1) { 
     $url = 'https://graph.facebook.com/' . key($friends_rank_data) . '/?fields=gender&access_token=' . $access_token ; 
     $contents = file_get_contents($url); 
     if($contents) 
     { 
      $dataa = json_decode($contents, true); 
     } 
    } 

    array_push($scores, array( 
     "id" => key($friends_rank_data), 
     "likes" => $user['likes'], 
     "comments" => $user['comments'], 
     "posts" => $user['posts'], 
     "sex" => $dataa['data']['0']['gender'],  
    ); 
} 
+0

您的答案和我的答案之間的區別是,像「性」和「網址」這樣的字段將被設置,即使它們爲空或不存在。在我的解決方案中,未使用的字段從未設置爲開頭。所有這些都是OP正在尋找的問題。好答案。 – Dutchie432

+0

@ Dutchie432的OP從URL中提取內容,所以我假定它不具有陣列中要被設置:) – Andy

+0

陣列 ( [0] =>數組 ( [ID] => 100000382928634 [喜歡] = > 10 [評論] => 6 [文章] => 41 [性愛] => ) ) – user3186365

1

嘗試這樣的事情。爲每個新項目創建一個數組,並首先設置您知道的值。然後,您可以根據您的if聲明有條件地爲其添加值。然後,一旦數組項目被構建,就將其粘貼到scores陣列上。

<?php 

foreach($top_9_friends as $key => $user){ 

    //Temporary Array with entity proprties 
    $new_array_item = array( 
     "id" => key($friends_rank_data), 
     "likes" => $user['likes'], 
     "comments" => $user['comments'], 
     "posts" => $user['posts'] 
    ); 

    //Conditionally Add values based on IF logic 
    if(1 == 1) { 
     $new_array_item['url'] = 'https://graph.facebook.com/'.key($friends_rank_data).'/?fields=gender&access_token='.$access_token ; 

     $contents = file_get_contents($new_array_item['url']); 
     if($contents) { 
      $dataa = json_decode($contents, true); 
      $new_array_item['sex'] = $dataa['data']['0']['gender']; 
     } 
    } 
    //Tack Temporary Array Item onto $scores 
    $scores[]=$new_array_item; 
} 

?> 
+0

警告:file_get_contents()[function.file-get-contents]:文件名不能爲空:$ contents = file_get_contents($ url); – user3186365

+0

我的錯誤。有一個錯字 - 嘗試新的代碼。謝謝。 – Dutchie432