簡體   English   中英

PHP數組的幾個問題

[英]A couple of issues with PHP arrays

這是我的代碼應該如何工作的。 我從一些遠程JSON URL中提取數據,並將其解碼回普通數組。 然后,我遍歷這些數組並創建一個組合數組。 循環時,我在組合數組內執行array_search,以查看username的值是否已存在並返回鍵。 如果返回了一個鍵,那么我會將來自該鍵的數據與循環數據進行合並。 如果搜索返回false,則將循環數據添加到數組的末尾。

我遇到了幾個問題,它們可能是相關的,但我不確定。

首先,在我的代碼中,當我運行array_search時,它破壞了代碼。 其次,如果我在array_search if語句上方var_dump主數組,那么該數組將填充循環的第一輪,但是當我從轉儲中查看數組的結構時,我看到該數組開始時很奇怪,我沒有不知道為什么。

這是代碼

    $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的輸出,請注意,數組以array()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',
      ),
    ),
  ),
)

所有人都知道我走了一條不同的路。 與其嘗試使事情變得如此復雜,不如我簡化了數組並完全擺脫了array_search。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM