2013-08-28 86 views
0

我有一個表:加入表數據

id title  type expl bubble_content onfocus req dorder label mirror 
1 Fullname 1  1  Your fullname Yes  0  0  0  NULL 

隨後的另一個表:

id  fieldid relid dorder 
4  1   2  0 
5  1   1  0 

我怎麼會加入兩個表,這樣的結果會是這樣的:

0 => array(
    'id' => 1, 
    'title' => 'Fullname', 
    .... etc .... 
    'relid' => 2, 
    'relid' => 1), 
1 => array(
    .... etc .... 
)) 

我試過使用INNER JOIN/LEFT JOIN,但是這會爲每個relid產生兩行/數組,我真的很喜歡如上所述,使特定字段id的所有數據存在於相同陣列內。

+0

你不能有兩個相同密鑰的陣列,這是沒有意義的。 –

+0

你如何加入表格?值1和2來自哪裏? –

+0

@JakubKania顯然。我問是否沒有辦法使用SQL – Kieran

回答

1

在數組中不能有2個具有相同名稱的鍵。在你的例子中你有2'relid',第二個將覆蓋第一個。

,使其合併這些行成一個你可以編寫PHP:

$output = array(); 
    while ($row = mysql_fetch_assoc($result)) 
    { 
     // Capture all values for this row first. 
     // If this is the new row from the first table, store. 
     if (!isset($output[$row['id']])) 
     { 
      $output[$row['id']] = $row; 

      // Make a new array for relids. 
      $output[$row['id']]['relids'] = array(); 
     } 


     $output[$row['id']]['relids'][] = $row['relid']; 

    } 

你的輸出數組看起來就像這樣:

0 => array(
'id' => 1, 
'title' => 'Fullname', 
.... etc .... 
'relids' => array(2, 1), 
1 => array(
.... etc .... 
))