2015-05-29 288 views
-1

我查詢我的數據庫與LEFT JOIN獲得以下數組:PHP創建多維數組

Array 
(
    [invoice_number] => 000010 
    [invoice_date] => 1432764000 
    [country] => 115 
    [fao] => Rick 
    [company_name] => Chubbs 
    [address_line_1] => kjhk 
    [address_line_2] => jh 
    [town] => kjh 
    [postcode] => kjh 
    [filename] => INV-1432820860.pdf 
    [id] => 11 
    [description] => dfgh 
    [rates] => 2 
    [quantity] => 3 
    [price] => 6 
    [created] => 0 
    [country_name] => Kazakhstan 
) 
Array 
(
    [invoice_number] => 000010 
    [invoice_date] => 1432764000 
    [country] => 115 
    [fao] => Rick 
    [company_name] => Chubbs 
    [address_line_1] => kjhk 
    [address_line_2] => jh 
    [town] => kjh 
    [postcode] => kjh 
    [filename] => INV-1432820860.pdf 
    [id] => 18 
    [description] => biscuits 
    [rates] => 2 
    [quantity] => 3 
    [price] => 6 
    [created] => 0 
    [country_name] => Kazakhstan 
) 

我通過這次要循環並刪除重複,所以是這樣的:

Array 
    (
     ['inv_details'] => array(
      [invoice_number] => 000010 
      [invoice_date] => 1432764000 
      [country] => 115 
      [fao] => Rick 
      [company_name] => Chubbs 
      [address_line_1] => kjhk 
      [address_line_2] => jh 
      [town] => kjh 
      [postcode] => kjh 
      [filename] => INV-1432820860.pdf 
     ) 
     ['items'] => array(
      [0] => array(
       [description] => dfgh 
       [rates] => 2 
       [quantity] => 3 
       [price] => 6 
       [created] => 0 
      ) 
      [1] => array(
       [description] => biscuits 
       [rates] => 2 
       [quantity] => 3 
       [price] => 6 
       [created] => 0 
      ) 

     ) 
    ) 

我有這樣的代碼的時刻,但該項目數組不加入兩個數組中的最後一項ovewrites第一個,我不與2個數組中的項目,剛一結束:

$i = 0; 

foreach($res->fetchAll(PDO::FETCH_ASSOC) as $row){ 

    $form = array(); 

    $form['title'] = $row['invoice_number']; 
    $form['inv_details']['invoice_date'] = array('value'=>gmdate('d/m/Y', $row['invoice_date']), 'type'=>'date'); 
    $form['inv_details']['company_name'] = array('value' => $row['company_name'], 'type' => 'text'); 
    $form['inv_details']['fao'] = array('value' => $row['fao'], 'type' => 'text'); 
    $form['inv_details']['address_line_1'] = array('value' => $row['address_line_1'], 'type' => 'text'); 
    $form['inv_details']['address_line_2'] = array('value' => $row['address_line_2'], 'type' => 'text'); 
    $form['inv_details']['town'] = array('value' => $row['town'], 'type' => 'text'); 
    $form['inv_details']['postcode'] = array('value' => $row['postcode'], 'type' => 'text'); 
    //$form['inv_details']['countries'] = $this->crm_account_address->getCountries(); 
    $form['country'] = $row['country_name']; 
    $form['country_id'] = $row['id']; 

    $form['items'][$i]['description'] = $row['description']; 
    $form['items'][$i]['rates'] = $row['rates']; 
    $form['items'][$i]['quantity'] = $row['quantity']; 
    $form['items'][$i]['price'] = $row['price']; 

    //counter 
    $i++; 

} 

任何幫助將是偉大的!謝謝

+2

*我查詢我的數據庫......並刪除重複*修復你的查詢語句,看看組 – donald123

+2

只是移動'$ form = array();'上面的for循環不在裏面 –

回答

0

在您的代碼中,每次發生迭代時都會初始化數組$form。所以之前的數據是用空白數組重新寫入的。

因此,保持了數組初始化上面你的for循環,使其在第一這樣

$i = 0; 

$form = array();  

foreach($res->fetchAll(PDO::FETCH_ASSOC) as $row){ 

    $form['title'] = $row['invoice_number']; 
    $form['inv_details']['invoice_date'] = array('value'=>gmdate('d/m/Y', $row['invoice_date']), 'type'=>'date'); 
    $form['inv_details']['company_name'] = array('value' => $row['company_name'], 'type' => 'text'); 
    $form['inv_details']['fao'] = array('value' => $row['fao'], 'type' => 'text'); 
    $form['inv_details']['address_line_1'] = array('value' => $row['address_line_1'], 'type' => 'text'); 
    $form['inv_details']['address_line_2'] = array('value' => $row['address_line_2'], 'type' => 'text'); 
    $form['inv_details']['town'] = array('value' => $row['town'], 'type' => 'text'); 
    $form['inv_details']['postcode'] = array('value' => $row['postcode'], 'type' => 'text'); 
    //$form['inv_details']['countries'] = $this->crm_account_address->getCountries(); 
    $form['country'] = $row['country_name']; 
    $form['country_id'] = $row['id']; 

    $form['items'][$i]['description'] = $row['description']; 
    $form['items'][$i]['rates'] = $row['rates']; 
    $form['items'][$i]['quantity'] = $row['quantity']; 
    $form['items'][$i]['price'] = $row['price']; 

    //counter 
    $i++; 

} 

初始化這將工作

+0

不能相信我把$ form數組放在循環中!愚蠢的錯誤!謝謝您的幫助! – Richard