-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}}?

7
  • 1
    Both echos 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']) . ' '; } Commented Dec 1, 2017 at 15:35
  • And where it the typo? There is none @FirstOne Commented Dec 1, 2017 at 15:39
  • 1
    @RoccoTheTaco as shown in both answers, you can't have two bodies in a foreach. That's a typo! Commented Dec 1, 2017 at 15:41
  • 1
    Duplicate: 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] Commented Dec 1, 2017 at 15:43
  • 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. Commented Dec 1, 2017 at 15:53

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This is probably what the op's looking for. I don't think it's 2 echos. +1
Thanks SO MUCH for the help, this is exactly what I needed.
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.)

2 Comments

This doubles the output of the icons and renders them twice?
@RoccoTheTaco: You have two echo statements, were you not trying to output two things? I'll update the answer with what it sounds like you mean...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.