2017-07-26 78 views
1

我有一个数组,看起来像这样:使用数组拼接换出数组的最后一个值?

Array ([0] => Credit Card Type [1] => MasterCard) 
Array ([0] => Credit Card Number [1] => xxxx-1111) 
Array ([0] => Processed Amount [1] => $106.91) 
Array ([0] => Transaction Id [1] => 5011056094736597703015) 
Array ([0] => AVS Response [1] => Z (Street address does not match, but 5-digit postal code matches.)) 
Array ([0] => CVN Response [1] => M (Card verification number matched.)) 
Array ([0] => Merchant Reference Code [1] => 25f11646823dc7488b48c04491335936) 

我使用print_r(array($_label, $_value));显示以上。

我想换出商户参考码值,这是长字母数字号码。

这是一个Magento的构建所以我假设我会附和

$order = Mage::getModel('sales/order')->load($orderId); 

echo $order->getIncrementId(); 

什么将是最适当的方式GE完成这项工作?

array_splice or array_push

任何帮助将不胜感激。谢谢。

<div class="cards-list"> 
    <?php if (!$this->getHideTitle()): ?> 
     <div class="bold"><?php echo $this->escapeHtml($this->getMethod()->getTitle()) ?></div> 
     <?php endif;?> 
</div> 
<?php 
    $cards = $this->getCards(); 
    $showCount = count($cards) > 1; 
?> 
<?php foreach ($cards as $key => $card): ?> 

    <?php if ($showCount): ?> 
     <span><?php echo sprintf($this->__('Credit Card %s'), $key + 1); ?></span> 
     <?php endif;?> 
    <table class="info-table<?php if ($showCount):?> offset<?php endif;?>"> 
     <tbody> 
      <?php foreach ($card as $_label => $_value):?> 
       <tr> 
        <td><?php echo $this->escapeHtml($_label)?>:</td> 
        <td><?php echo nl2br(implode($this->getValueAsArray($_value, true), "\n"))?></td> 

       </tr> 
       <?php endforeach; ?> 
     </tbody> 
    </table> 
    <?php endforeach; ?> 
+0

你能澄清你所说的 “换出” 的意思。根据你对array_splice的引用,我假设你正在寻找从你的数组中删除“Merchant Reference Code”元素,并添加另一个元素与订单ID? –

+0

这是正确的@ ever.wakeful – Singleton

+0

你可以添加生成你的'$ _label'和'$ _value'的代码吗? – sv3n

回答

2

OK,所以根据您所提供的输出的print_r,我会认为你是通过一个数组,看起来像下面的循环和打印键($ _label)和价值($ _值)。

$data = array(
    'Credit Card Type' => 'MasterCard', 
    'Credit Card Number' => 'xxxx-1111', 
    'Processed Amount' => '$106.91', 
    'Transaction Id'=> '5011056094736597703015', 
    'AVS Response' => 'Z (Street address does not match, but 5-digit postal code matches.)', 
    'CVN Response' => 'M (Card verification number matched.)', 
    'Merchant Reference Code' => '25f11646823dc7488b48c04491335936' 
); 

那么为什么不只是设置商家参考代码键,并添加你想要的任何键/值到数组中。例如:

unset($data['Merchant Reference Code']); 
$data['Order Id'] = $order->getIncrementId(); 
+0

当我用上面的代码测试它时,它打破了页面的下半部分。也许我做错了什么? – Singleton

+0

我想这取决于您添加该代码的位置。很难只用你正在编写的部分代码进行分析。你在哪里打电话Mage :: getModel('sales/order') - > load($ orderId); ? –

1

我想你可以替换:

<?php foreach ($card as $_label => $_value):?> 
<tr> 
    <td><?php echo $this->escapeHtml($_label)?>:</td> 
    <td><?php echo nl2br(implode($this->getValueAsArray($_value, true), "\n"))?></td> 

</tr> 
<?php endforeach; ?> 

有:

<?php foreach ($card as $_label => $_value): ?> 
<?php if ($_label === 'Merchant Reference Code') { 
    continue; 
} ?> 
<tr> 
    <td><?php echo $this->escapeHtml($_label)?>:</td> 
    <td><?php echo nl2br(implode($this->getValueAsArray($_value, true), "\n"))?></td> 

</tr> 
<?php endforeach; ?> 
<tr> 
    <td>Order ID:</td> 
    <td><?php echo $order->getIncrementId();?></td> 
</tr> 

注:也许添加$this->__()翻译 “订单ID” 和“商家参考代码“


编辑:回答评论

如果模板块类从Mage_Core_Block_Abstract继承您可以使用$this->__('some String)使用Magentos默认的方法来翻译的东西。

所以第一件事就是更换

<?php if ($_label === 'Merchant Reference Code') { 

随着

<?php if ($_label === $this->__('Merchant Reference Code')) { 

这使得被tranlated给客户语言indepented这此检查的语言。对于德语,它将首先翻译为<?php if ($_label === Refferenz Code) {,它仍然可以工作。 “订单ID:”相同。

为了支持不同的语言......

  • 可用

    "Merchant Reference Code";"Translate string" 
    
  • 化妆tranlation文件添加My_Module.csvapp/locale/{LANG_ISO}/app/code/{pool}/My/Module/ect/config.xml添加这globalfrontendadminhtml部分

    <translate> 
        <modules> 
         <My_Module> 
          <files> 
           <default>My_Module.csv</default> 
          </files> 
         </My_Module> 
        </modules> 
    </translate> 
    
  • 添加帮手 “使能” 翻译,将其添加到app/code/{pool}/My/Module/Helpers/Data.php

    class My_Module_Helper_Data extends Mage_Core_Helper_Abstract 
    { 
        protected $_moduleName = 'My_Module'; 
    } 
    
+0

它打破了页面。请解释最后一部分$ this - > __()。 – Singleton

+1

@Singleton更新了答案。 – sv3n

相关问题