-2

I have a foreach loop in a PHP script that outputs icons / links from a database. I've used this question to use str_replace to change some values after the icons are rendered.

foreach ($icons as $ic)
    { echo str_replace('{{EMP_NO}}', $_SESSION['EmpNo'], $ic['url']) . ' '; }

I need to do a similar operation on a value {{FIRST_NAME}} but when I append with a second str_replace the second command / value is ignored. Like this:

foreach ($icons as $ic)

    { echo str_replace('{{EMP_NO}}', $_SESSION['EmpNo'], $ic['url']) . ' '; }
    { echo str_replace('{{FIRST_NAME}}', $_SESSION['FirstName'], $ic['url']) . ' '; }

When I try to combine them like this it doubles the icons output which I don't want:

foreach ($icons as $ic) { echo str_replace('{{EMP_NO}}', $_SESSION['EmpNo'], $ic['url']) . ' '; echo str_replace('{{FIRST_NAME}}', $_SESSION['FirstName'], $ic['url']) . ' '; }

How do I also str_replace {{FIRST_NAME}}?

FirstOne
  • 6,033
  • 7
  • 26
  • 45
Rocco The Taco
  • 3,695
  • 13
  • 46
  • 79
  • 1
    Both `echo`s should be inside the loop body: `foreach ($icons as $ic) { echo str_replace('{{EMP_NO}}', $_SESSION['EmpNo'], $ic['url']) . ' '; echo str_replace('{{FIRST_NAME}}', $_SESSION['FirstName'], $ic['url']) . ' '; }` – Genti Saliu Dec 01 '17 at 15:35
  • And where it the typo? There is none @FirstOne – Rocco The Taco Dec 01 '17 at 15:39
  • Show us an example of what `$ic['url']` looks like before replacing. – Genti Saliu Dec 01 '17 at 15:39
  • 1
    @RoccoTheTaco as shown in both answers, you can't have two bodies in a `foreach`. That's a typo! – FirstOne Dec 01 '17 at 15:41
  • 1
    Duplicate: [Search and replace multiple values with multiple/different values in PHP5?](https://stackoverflow.com/questions/535143/search-and-replace-multiple-values-with-multiple-different-values-in-php5). Another dup here: [How to replace multiple items from a text string in PHP? [duplicate\]](https://stackoverflow.com/questions/9393885/how-to-replace-multiple-items-from-a-text-string-in-php) – FirstOne Dec 01 '17 at 15:43
  • @FirstOne neither of those questions are clear in regards to my specific question..they are actually poorly formed and answered. I would never have used either answer/question to formulate my own solution. My question and the resulting answer is much more clear on the usage of str_replace in a loop – Rocco The Taco Dec 01 '17 at 15:50
  • 2
    @RoccoTheTaco Agree to disagree. The linked question and answers show a succinct example on how to use the function, just like the manual (which you failed to consult). Your case only adds complexity by using an array and a loop. – FirstOne Dec 01 '17 at 15:53

2 Answers2

3

str_replace can replace array of values to array of other values:

foreach ($icons as $ic) {
    echo str_replace(
        array('{{EMP_NO}}', '{{FIRST_NAME}}'),
        array($_SESSION['EmpNo'], $_SESSION['FirstName']), 
        $ic['url']) . ' '
    );
}

And as already mentioned in @David's answer, though your code has a correct syntax, but its' logic is flawed.

u_mulder
  • 54,101
  • 5
  • 48
  • 64
1

You've structured your loop incorrectly. If you re-align the brackets, you see that you have this:

foreach ($icons as $ic)
{
    echo str_replace('{{EMP_NO}}', $_SESSION['EmpNo'], $ic['url']) . ' ';
}
{
    echo str_replace('{{FIRST_NAME}}', $_SESSION['FirstName'], $ic['url']) . ' ';
}

Basically, you've given the same loop two bodies. What that actually does is execute the first body as the loop itself, then the second "body" (block of code in random curly braces) is simply executed once after the loop, like any other code.

A loop has only one body:

foreach ($icons as $ic) {
    echo str_replace('{{EMP_NO}}', $_SESSION['EmpNo'], $ic['url']) . ' ';
    echo str_replace('{{FIRST_NAME}}', $_SESSION['FirstName'], $ic['url']) . ' ';
}

Edit: Based on a comment below, it sounds like you really only want to output one line instead of two. In that case you don't want to echo twice. But you do still want to replace twice. A first pass might just take the output of the first str_replace() as the input for the second. However, str_replace() also accepts arrays (so it internally "replaces twice"):

foreach ($icons as $ic) {
    echo str_replace(array('{{EMP_NO}}', '{{FIRST_NAME}}'), array($_SESSION['EmpNo'], $_SESSION['FirstName']), $ic['url']) . ' ';
);

(Note: It looks like another answer suggests this as well while I was creating this one.)

David
  • 208,112
  • 36
  • 198
  • 279