2017-02-28 134 views
0

這是我的代碼應該如何工作。我從一些遠程JSON URL中提取數據並將它們解碼回正常的數組。然後我遍歷這些數組並創建一個組合數組。循環時,我在組合數組內執行array_search查看用戶名的值是否已經存在並返回鍵。如果返回一個密鑰,那麼我將該密鑰的數據與循環數據結合起來。如果搜索返回false,則將循環數據添加到數組的末尾。PHP array的幾個問題

有幾個問題我有,他們可能有關,但我不知道。

首先,在我的代碼中,當我的array_search運行時,它破壞了代碼。 其次,如果我var_dump在array_search if語句之上的主數組,然後該數組填充循環的第一輪,但是當我從轉儲看數組的結構時,我看到該數組開始出奇怪的,我不知道爲什麼。

下面是代碼

$master_user_array = array(); 
    foreach($url_list AS $url) { 
     $json = file_get_contents($url."?key=".self::AUTH_KEY); 
    $data = json_decode($json, true); 

    $user_count = 0; 
     foreach($data['user'] AS $user) {echo highlight_string(var_export($master_user_array, true)); 
    if(count($master_user_array) > 0) { 
     $key = array_search($user['username'], array_column($master_user_array, 'username')); 
      } else { 
       $key = false; 
      } 

    if(false !== $key) { 
     $master_user_array[$key]['username'] = $user['username']; 
     $master_user_array[$key]['email'] = $user['email']; 

     $master_user_array[$key]['total']['counttoday'] += $user['counttoday']; 
       $master_user_array[$key]['total']['countweek'] += $user['countweek']; 
       $master_user_array[$key]['total']['countmonth'] += $user['countmonth']; 
       $master_user_array[$key]['total']['countyear'] += $user['countyear']; 
       $master_user_array[$key]['total']['counttotal'] += $user['counttotal']; 

     $master_user_array[$key]['sites'][$data['siteurl']]['counttoday'] = $user['counttoday']; 
       $master_user_array[$key]['sites'][$data['siteurl']]['countweek'] = $user['countweek']; 
       $master_user_array[$key]['sites'][$data['siteurl']]['countmonth'] = $user['countmonth']; 
       $master_user_array[$key]['sites'][$data['siteurl']]['countyear'] = $user['countyear']; 
       $master_user_array[$key]['sites'][$data['siteurl']]['counttotal'] = $user['counttotal']; 
      } else { 
     $master_user_array[$user_count]['username'] = $user['username']; 
     $master_user_array[$user_count]['email'] = $user['email']; 

     $master_user_array[$user_count]['total']['counttoday'] = $user['counttoday']; 
       $master_user_array[$user_count]['total']['countweek'] = $user['countweek']; 
       $master_user_array[$user_count]['total']['countmonth'] = $user['countmonth']; 
       $master_user_array[$user_count]['total']['countyear'] = $user['countyear']; 
       $master_user_array[$user_count]['total']['counttotal'] = $user['counttotal']; 

     $master_user_array[$user_count]['sites'][$data['siteurl']]['counttoday'] = $user['counttoday']; 
       $master_user_array[$user_count]['sites'][$data['siteurl']]['countweek'] = $user['countweek']; 
       $master_user_array[$user_count]['sites'][$data['siteurl']]['countmonth'] = $user['countmonth']; 
       $master_user_array[$user_count]['sites'][$data['siteurl']]['countyear'] = $user['countyear']; 
       $master_user_array[$user_count]['sites'][$data['siteurl']]['counttotal'] = $user['counttotal']; 

     $user_count++; 
      } 
     } 
    } 

這裏是從通知的var_dump輸出的陣列陣列( )1開始。如果我擺脫array_search和代碼不那麼這個突破數組的一部分被添加到循環的每一輪的開始。總是與1.

array (
) 1 array (
    '' => 
    array (
    'username' => 'somename', 
    'email' => 'someemail', 
    'total' => 
    array (
     'counttoday' => 0, 
     'countweek' => 0, 
     'countmonth' => 0, 
     'countyear' => 0, 
     'counttotal' => 3, 
    ), 
    'sites' => 
    array (
     '' => 
     array (
     'counttoday' => 0, 
     'countweek' => 0, 
     'countmonth' => 0, 
     'countyear' => 0, 
     'counttotal' => '3', 
    ), 
    ), 
), 
) 

回答

0

大家都知道我走了一條不同的路。我沒有試圖讓事情變得如此複雜,而是簡化了數組並完全擺脫了array_search。