2015-09-07 90 views
0

我想打印一個數組。我需要幫助打印一個php multidementional陣列

第一個例子。

$result2 = mysql_query("SELECT 
ps_product_attribute.id_product_attribute AS attribute_id, 
ps_advanced_attributes.pack_product_id AS base_product, 
ps_advanced_attributes.pack_product_qty AS qty_base_product, 
ps_stock_available.quantity AS base_product_on_hand, 
FLOOR(ps_stock_available.quantity/ps_advanced_attributes.pack_product_qty) AS available_to_customer 
FROM ps_product_attribute 
JOIN ps_advanced_attributes ON ps_advanced_attributes.attribute_id = ps_product_attribute.id_product_attribute 
JOIN ps_stock_available ON ps_stock_available.id_product = ps_advanced_attributes.pack_product_id 
ORDER BY ps_product_attribute.id_product_attribute ASC, ps_stock_available.quantity DESC"); 

echo '<pre>'; 
WHILE($db_field2 = mysql_fetch_array($result2)){ 
print_r($db_field2);} 
echo '</pre>'; 

給出了這樣的結果......只有

Array(
    [0] => 6703 
    [attribute_id] => 6703 
    [1] => 382 
    [base_product] => 382 
    [2] => 1 
    [qty_base_product] => 1 
    [3] => 69 
    [base_product_on_hand] => 69 
    [4] => 69 
    [available_to_customer] => 69 
) 

這一結果

例如2 ...

$result2 = mysql_query("SELECT 
ps_product_attribute.id_product_attribute AS attribute_id, 
ps_advanced_attributes.pack_product_id AS base_product, 
ps_advanced_attributes.pack_product_qty AS qty_base_product, 
ps_stock_available.quantity AS base_product_on_hand, 
FLOOR(ps_stock_available.quantity/ps_advanced_attributes.pack_product_qty) AS available_to_customer 
FROM ps_product_attribute 
JOIN ps_advanced_attributes ON ps_advanced_attributes.attribute_id = ps_product_attribute.id_product_attribute 
JOIN ps_stock_available ON ps_stock_available.id_product = ps_advanced_attributes.pack_product_id 
ORDER BY ps_product_attribute.id_product_attribute ASC, ps_stock_available.quantity DESC"); 

echo '<pre>'; 
WHILE($db_field2 = mysql_fetch_array($result2)){ 
print_r($db_field2);} 
echo '</pre>'; 

籃網這樣的結果......

Array 

(

    [0] => 6703 
    [attribute_id] => 6703 
    [1] => 382 
    [base_product] => 382 
    [2] => 1 
    [qty_base_product] => 1 
    [3] => 69 
    [base_product_on_hand] => 69 
    [4] => 69 
    [available_to_customer] => 69 
) 

Array 

(

    [0] => 6703 
    [attribute_id] => 6703 
    [1] => 103 
    [base_product] => 103 
    [2] => 1 
    [qty_base_product] => 1 
    [3] => 4 
    [base_product_on_hand] => 4 
    [4] => 4 
    [available_to_customer] => 4 
) 
Array 
(

    [0] => 6703 
    [attribute_id] => 6703 
    [1] => 471 
    [base_product] => 471 
    [2] => 1 
    [qty_base_product] => 1 
    [3] => 0 
    [base_product_on_hand] => 0 
    [4] => 0 
    [available_to_customer] => 0 
) 
Array 
(

    [0] => 6704 
    [attribute_id] => 6704 
    [1] => 103 
    [base_product] => 103 
    [2] => 1 
    [qty_base_product] => 1 
    [3] => 4 
    [base_product_on_hand] => 4 
    [4] => 4 
    [available_to_customer] => 4 
) 
Array 
(

    [0] => 6704 
    [attribute_id] => 6704 
    [1] => 397 
    [base_product] => 397 
    [2] => 1 
    [qty_base_product] => 1 
    [3] => 0 
    [base_product_on_hand] => 0 
    [4] => 0 
    [available_to_customer] => 0 
) 
Array 
(

    [0] => 6704 
    [attribute_id] => 6704 
    [1] => 465 
    [base_product] => 465 
    [2] => 1 
    [qty_base_product] => 1 
    [3] => 0 
    [base_product_on_hand] => 0 
    [4] => 0 
    [available_to_customer] => 0 
) 
Array 
(

    [0] => 6705 
    [attribute_id] => 6705 
    [1] => 103 
    [base_product] => 103 
    [2] => 1 
    [qty_base_product] => 1 
    [3] => 4 
    [base_product_on_hand] => 4 
    [4] => 4 
    [available_to_customer] => 4 
) 
Array 
(

    [0] => 6705 
    [attribute_id] => 6705 
    [1] => 533 
    [base_product] => 533 
    [2] => 1 
    [qty_base_product] => 1 
    [3] => 2 
    [base_product_on_hand] => 2 
    [4] => 2 
    [available_to_customer] => 2 
) 
Array 
(

    [0] => 6705 
    [attribute_id] => 6705 
    [1] => 469 
    [base_product] => 469 
    [2] => 1 
    [qty_base_product] => 1 
    [3] => 0 
    [base_product_on_hand] => 0 
    [4] => 0 
    [available_to_customer] => 0 
) 
Array 
(

    [0] => 6706 
    [attribute_id] => 6706 
    [1] => 395 
    [base_product] => 395 
    [2] => 1 
    [qty_base_product] => 1 
    [3] => 41 
    [base_product_on_hand] => 41 
    [4] => 41 
    [available_to_customer] => 41 
) 
Array 
(

    [0] => 6706 
    [attribute_id] => 6706 
    [1] => 103 
    [base_product] => 103 
    [2] => 1 
    [qty_base_product] => 1 
    [3] => 4 
    [base_product_on_hand] => 4 
    [4] => 4 
    [available_to_customer] => 4 
) 

Array 
(

    [0] => 6706 
    [attribute_id] => 6706 
    [1] => 468 
    [base_product] => 468 
    [2] => 1 
    [qty_base_product] => 1 
    [3] => 0 
    [base_product_on_hand] => 0 
    [4] => 0 
    [available_to_customer] => 0 
) 

正如你所看到的,很多結果,但不是我期待看到的。在這兩個例子中,我都得到重複的數據。

我在這裏做錯了什麼?我認爲第一個例子應該已經打印了數組的全部內容。

+0

你是說hte查詢我們錯誤還是顯示?即時消息不明確至於什麼問題 –

回答

0

你應該先提取所有結果到數組,然後纔打印出來

$result = []; 
while ($row = mysql_fetch_assoc($result2)){ 
    $result[] = $row; 
} 
print_r($result); 

UPD:除掉重複數據的使用mysql_fetch_assoc,而不是mysql_fetch_array

UPD2:考慮使用mysqli擴展,而不是的MySQL。 PHP.net

+0

謝謝,這解決了第一個問題。任何想法擺脫重複的數據? – Coathanger

+0

@Coathanger是的,更新了我的答案 – Mizax

+0

謝謝,這解決了主要問題。關於如何擺脫重複dataArray中的 ( [0] => 6703 [attribute_id] => 6703 [1] => 103 [base_product] => 103 [2] => 1的任何想法 [qty_base_product] => 1 [3] => 4 [base_product_on_hand] => 4 [4] => 4 [available_to_customer] => 4 ) – Coathanger