2013-04-06 48 views
2

我不明白我的代碼有什麼問題。我在Joomla下模擬一個網站! 2.5.9和VirtueMart 2.0.20b。從PHP中的數組獲取密鑰Foreach語句

我'上目錄頁tplname/html/com_virtuemart/category/default.php產品頁面tplname/html/com_virtuemart/productdetails/default.php工作。

在這兩個頁面上,我需要顯示以%計的稅:即:20%的稅。

對於產品頁面是:

$product->prices['Tax']; 

對於分類頁面是:

<?php foreach ($products as $product):?> 
    <li> 
     <?php echo $product->prices['Tax'];?> 
    </li> 
<?php endforeach;?> 

在這兩個做一個var_dump()它給了我這樣的輸出:

array 
    204 => // <- Remember this number as FIRST ARRAY 
    array 
     0 => string 'TAXNAME_TITLE' (length=12) 
     1 => string '10.0000' (length=7) // <- I need this value! 
     2 => string '+%' (length=2) 
     3 => string '1' (length=1) 
     4 => string '47' (length=2) 
     5 => string '' (length=0) 
     6 => string '1' (length=1) 
     7 => string '204' (length=3) 

要獲得我需要的價值,我這樣做:

$productPriceTaxUnit = reset($product->prices['Tax']); 
$productPriceTaxUnit = $productPriceTaxUnit['1']; 
$productPriceTaxUnit = JText::_('COM_VIRTUEMART_PRODUCT_BASEPRICE_WITHTAX') . ' ' .number_format($productPriceTaxUnit, '2', '.', '') . '%'; 
echo $productPriceTaxUnit; 

它工作正常,但一個類別頁面,FIRST ARRAY等於204上gaves我一個錯誤,但如果它等於1那麼一切都很好。

有人看到錯誤的一步,我在這裏做?

在此先感謝

====== ** 1 **修訂========

這裏是一個SQL調試:

SELECT SQL_CALC_FOUND_ROWS l.`virtuemart_product_id` 
    FROM `ar9hu_virtuemart_products_sk_sk` as l JOIN `ar9hu_virtuemart_products` AS p using (`virtuemart_product_id`) 
    LEFT JOIN `ar9hu_virtuemart_product_categories` as pc 
    ON p.`virtuemart_product_id` = `pc`.`virtuemart_product_id` 
    LEFT JOIN `ar9hu_virtuemart_categories_sk_sk` as c 
    ON c.`virtuemart_category_id` = `pc`.`virtuemart_category_id` 
    LEFT JOIN `ar9hu_virtuemart_product_shoppergroups` 
    ON p.`virtuemart_product_id` = `ar9hu_virtuemart_product_shoppergroups`.`virtuemart_product_id` 
    LEFT 
    OUTER JOIN `ar9hu_virtuemart_shoppergroups` as s 
    ON s.`virtuemart_shoppergroup_id` = `ar9hu_virtuemart_product_shoppergroups`.`virtuemart_shoppergroup_id` 
    WHERE (p.`published`="1" 
    AND `pc`.`virtuemart_category_id` = 9 
    AND `pc`.`virtuemart_category_id` > 0 
    AND (s.`virtuemart_shoppergroup_id`= "1" OR s.`virtuemart_shoppergroup_id` IS NULL)) 
    group by p.`virtuemart_product_id` 
    ORDER BY `p`.product_sku ASC 
    LIMIT 0, 10 

== ==== ** 2日修訂版** ========

我這是在分類頁面上得到的錯誤:

Notice: Trying to get property of non-object in D:\wamp\www\baranik\templates\baranik\html\com_virtuemart\category\default.php on line 119 
Call Stack 
# Time Memory Function Location 
1 0.0002 652096 {main}() ..\index.php:0 
2 0.0714 9131016 JSite->dispatch() ..\index.php:42 
3 0.0749 9542784 JComponentHelper::renderComponent() ..\application.php:197 
4 0.0785 9691392 JComponentHelper::executeComponent() ..\helper.php:351 
5 0.0789 9787480 require_once('D:\wamp\www\baranik\components\com_virtuemart\virtuemart.php') ..\helper.php:383 
6 0.0980 12819808 JController->execute() ..\virtuemart.php:99 
7 0.0980 12819888 VirtueMartControllerCategory->display() ..\controller.php:761 
8 0.0980 12821880 JController->display() ..\category.php:60 
9 0.1006 13209152 VirtuemartViewCategory->display() ..\controller.php:722 
10 0.2021 19796592 JView->display() ..\view.html.php:244 
11 0.2021 19796592 JView->loadTemplate() ..\view.php:205 
12 0.2031 19910088 include('D:\wamp\www\baranik\templates\baranik\html\com_virtuemart\category\default.php') ..\view.php:649 
+0

我不知道爲什麼,這有一個downvote。這似乎是一個有效的問題,提問者在其背後進行了有效的研究。 +1 – seanbreeden 2013-04-06 13:41:21

+1

你會得到什麼錯誤? – Barmar 2013-04-06 13:59:05

+0

@Barmar - 我已經更新了一個問題,請檢出第二次更新 – aspirinemaga 2013-04-06 18:34:23

回答

1

我不確定我是否正確理解你的問題,但我可以回答你的問題的標題。

「獲取從陣列PHP foreach語句中的一個關鍵」

你可以通過遍歷數組,並使用獲得的密鑰和值:

foreach($array as $key => $value){ 
    echo "$key has a value of $value<br>"; 
}