2016-04-11 45 views
1

我意識到有關於多維數組和foreach循環的一些問題,我花了數小時閱讀它們並試圖讓我自己的循環工作 - 沒有成功。如果解決方案是重複的,我會刪除我的問題(或者如果這是首選的,則鏈接到另一個問題)。現在多維數組循環與數組搜索

,面臨的挑戰:

  • 使用MYSQL返回結果的數組。結果來自關聯數組中的多個連接表。現在我需要將其轉換爲我需要的多維數組。
  • 我已經得到了大部分的工作,但我的問題是循環並添加新的項目到陣列中的正確位置。

下面是一些代碼:

//example of how array is setup in the way I want, this part works. 
foreach($results as $i => $a): 

    //some other code is here, see below. 

    $items[$i] = [ 
     "id" => $a['id'], 
     "itemid" => $a['itemid'], 
     "name" => $a['name'], 
     "def" => $a['def'], 
     "class" => $a['class'], 
     "timeline" => $a['timeline'], 
     "files" => [ 
      [0] => [ 
       "id" => $a['fileid'], 
       "name" => $a['filename'], 
       "path" => $a['filepath'], 
       "type" => $a['filetype'] 
      ] 
     ], 
     "tags" => [ 
      [0] => [ 
       "id" => $a['tagid'], 
       "name" => $a['tagname'] 
      ] 
     ] 
    ]; 
endforeach; 

然後我爲了嘗試了多種方式來遍歷只添加到「標籤」或「文件」,如果該項目「ID」是和最後一樣。下面是當前的代碼在我的編輯,不工作:

//inside foreach loop, before above code 
if($items[$i-1]['id'] == $a['id']): 
    //it is the same item, works to here. 
    if(in_array($a['filename'], $items[$i-1], FALSE)): 
     //add to files array for last item 
     $items[$i-1]['files'][] = [ 
      "id" => $a['fileid'], 
      "name" => $a['filename'], 
      "path" => $a['filepath'], 
      "type" => $a['filetype'] 
     ]; 
    elseif(in_array($a['tagname'], $items[$i-1], FALSE)): 
     //add to tags array for last item 
     $items[$i-1]['tags'][] = [ 
      "id" => $a['tagid'], 
      "name" => $a['tagname'] 
     ]; 
    endif; 
else:// else it does the code above 

正如你所看到的,我最近嘗試是使用in_array,我現在實現對多維數組不起作用。我的問題是,我不知道如何確定它是一個新的文件或新的標籤爲同一個項目。

最終,我想要一個包含多個'文件'和'標籤'的'項目'數組。我要去json_encode,然後在JS中使用它。

任何意見,如何得到這個工作或優化它,將不勝感激。

P.S.正如我上面提到的,我知道這個問題之前已經被問到 - 儘管我無法讓他們的解決方案爲我工作。如果解決方案是重複的(如對其他人沒有幫助),我將刪除此問題。感謝您的幫助,非常感謝!

+0

什麼是你的PHP版本?在PHP> = 5.5上,你可以使用['array_column'](http://php.net/manual/en/function.array-column.php):'in_array($ a ['filename'],array_column($ items [$ i-1] ['files'],'name'))' – fusion3k

+0

感謝提示@ fusion3k。我將閱讀關於該功能的文檔,並嘗試一下。 – turnfire

回答

0

不要使用「autoincrementing」數組索引,因爲它們很容易混淆。用你的數據庫ID,因爲它已經存在:

//example of how array is setup in the way I want, this part works. 
foreach($results as $i => $a): 
$items[$a['id']] = [ // THIS CHANGED. 
    "id" => $a['id'], 
    "itemid" => $a['itemid'], 
    ... 

現在,任何進一步的結果,你可以很容易地檢查,如果ID已經數組中:

if (isset($items[$a['id']])) { 
    // We had this id before, just add files/tags to it. 
    // Check first, if files subarray exists, if not: create it. 
    if (!isset($items[$a['id']]['files'])) { 
    $items[$a['id']]['files'] = array(); 
    } 
    $items[$a['id']]['files'][] = array(...); // add the new file. 
    // Repeat for tags. 
} 

如果您的結果可能會返回同一個文件不是一次一個id更多,你可以檢查,如果文件名是已經在那裏通過使用搜索功能:

$filename = $a['filename']; 
if (!searchFilename($filename, $items[$a['id']]['files']) { 
    // Filename is not in there, yet. 
    // Add file info. 
} 

function searchFilename($id, $array) { 
    foreach ($array as $key => $val) { 
    if ($val['filename'] === $id) { 
     return true; 
    } 
    } 
    return false; 
} 

同樣適用於標籤以類似的方式。

最後,如果你不想要的$items指數的ID,只要致電:

$items = array_values($items); 
+0

我想我明白我現在出錯了 - 使用索引。如果有超過1個附加文件/標籤,它正在查看數組中的錯誤索引。 謝謝你的解釋和時間!我很確定你的解決方案對我有用。我會在我的代碼中得到它時確認/標記解決方案:P (PS我應該刪除此問題還是保留它?該缺陷位於我的邏輯中= P隨意將意見留作評論。) – turnfire

+0

正確地,因爲'$ i'是遞增的,不管'$ a ['id']'之前是否處理過。 – Paul

+0

我做了一些改動,但你的解決方案是我需要的。我完全改變了我的數組安裝方式,使用文件/標籤ID作爲它們的索引。再次感謝保羅,你搖滾! – turnfire