从PHP Foreach语句中的数组中获取一个键

时间:2013-04-06 13:35:15

标签: php joomla2.5 virtuemart

我无法理解我的代码有什么问题。我想在Joomla下建立一个网站! 2.5.9和VirtueMart 2.0.20b。

我正在使用类别页面 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的类别页面上给我一个错误,但如果它等于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

====== **第二次更新** ========

这是我在类别页面上出现的错误:

    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

1 个答案:

答案 0 :(得分:1)

我不确定我是否理解你的问题,但我可以回答你问题的标题。

'从PHP Foreach语句中的数组中获取一个键'

您可以使用以下方法遍历数组并获取键和值:

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