Search code examples
phparraysjoomla

How to display comma in PHP array?


I am setting this array, and for the most part it works properly in my site. NOTE: This is a Joomla site and a VirtueMart PHP file for the email that the customer receives when ordering a product.

$addressBTOrder = array('email{br}', 'company{br}', 'title', 'first_name', 'last_name{br}', 'address_1{br}', 'address_2{br}', 'city', 'virtuemart_state_id', 'zip{br}', 'virtuemart_country_id{br}', 'phone_1{br}', 'phone_2{br}');

However, I need to add a comma (,) after CITY in the array to actually display a comma in the final output. If I just put the comma after city:

'city,'

... it does not work and the city output just doesn't display at all. I also tried the hex value for comma (%2c) but it didn't work either.

Here is the code that is creating the output:

<?php
        foreach ($addressBTOrder as $fieldname) {
            $fieldinfo = explode('{',$fieldname);
            if (!empty($this->userfields['fields'][$fieldinfo[0]]['value'])) { ?>
                <span class="values vm2<?php echo '-' . $this->userfields['fields'][$fieldinfo[0]]['name'] ?>" ><?php echo $this->escape($this->userfields['fields'][$fieldinfo[0]]['value']) ?></span> <?php
                if (isset($fieldinfo[1]) && $fieldinfo[1] == 'br}') { ?>
                    <br class="clear" /> <?php
                }
            }
        }
    ?>

What code do I need to put there to display a comma in the final output? For instance, I am making the line break work by using {br}. Thanks!


Solution

  • What I'm getting from your code is that you want to specify some sort of suffix after each output of the corresponding userfield value.

    A differentapproach is to make use of your CSS classes. This means you can properly style your content without adding extra elements.

    For example, going back to your original array without anything extra

    $addressBTOrder = ['email', 'company', 'title', 'city', ...];
    
    foreach ($addressBTOrder as $field) :
    $userField = $this->userfields['fields'][$field];
    if (!empty($userField['value'])) : ?>
    <span class="values vm2-<?= $userField['name'] ?>">
      <?= $this->escape($userField['value']) ?>
    </span>
    <?php endif; endforeach ?>
    

    and, since you're already adding CSS classes which I'm guessing are like vm2-email, vm2-company, etc...

    .vm2-email, .vm2-company {
      display: block; /* same as adding a newline */
    }
    .vm2-city:after {
      content: ",";
    }
    

    Original answer here...

    I would recommend using a more succinct data format. For example

    $br = '<br class="clear" />';
    $addressBTOrder = [[
        'key' => 'email',
        'suffix' => $br
    ], [
        'key' => 'company',
        'suffix' => $br
    ], [
        'key' => 'title',
        'suffix' => ''
    ], /* etc */ [
        'key' => 'city',
        'suffix' => ','
    ]];
    

    then you can iterate like this...

    <?php foreach ($addressBTOrder as $field) :
    $userField = $this->userfields['fields'][$field['key']];
    if (!empty($userField['value'])) : ?>
    <span class="values vm2-<?= $userField['name'] ?>">
      <?= $this->escape($userField['value']), $field['suffix'] ?>
    </span>
    <?php endif; endforeach ?>