【问题标题】:Using array splice to swap out the last value of an array?使用数组拼接换出数组的最后一个值?
【发布时间】:2017-07-26 23:19:01
【问题描述】:

我有一个如下所示的数组:

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();

完成工作最合适的方法是什么?

array_splicearray_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; ?>

【问题讨论】:

  • 你能澄清一下你所说的“换出”是什么意思吗?根据您对 array_splice 的引用,我假设您希望从数组中删除“商家参考代码”元素并添加另一个具有订单 ID 的元素?
  • 这是正确的@ever.wakeful
  • 您能否添加生成您的$_label$_value 的代码?
  • 当然@sv3n 我现在就这样做。

标签: php arrays magento array-splice


【解决方案1】:

好的,所以根据您提供的 print_r 输出,我将假设您正在循环一个如下所示的数组并打印键 ($_label) 和值 ($_value)。

$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();

【讨论】:

  • 当我用上面的代码测试它时,它会破坏页面的下半部分。也许我做错了什么?
  • 我猜这取决于您在哪里添加了该代码。仅使用您正在处理的部分代码很难判断。你在哪里调用 Mage::getModel('sales/order')->load($orderId); ?
【解决方案2】:

我认为你可以替换:

<?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-&gt;__()来翻译“Order ID”和“Merchant Reference Code”


编辑:回复评论

如果你的模板块类继承自Mage_Core_Block_Abstract,你可以使用$this-&gt;__('some String)来使用Magentos的默认方法来翻译。

所以第一件事就是替换

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

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

这使得这种检查语言独立于被翻译成客户语言。对于德语,它会首先被翻译成&lt;?php if ($_label === Refferenz Code) {,它仍然可以工作。与“订单 ID:”相同。

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

  • My_Module.csv 添加到app/locale/{LANG_ISO}/

    "Merchant Reference Code";"Translate string"
    
  • 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';
    }
    

【讨论】:

  • 它打破了页面。请解释最后一部分 $this->__()。
猜你喜欢
  • 2018-09-09
  • 2017-03-11
  • 1970-01-01
  • 1970-01-01
  • 2015-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多