3

Output for $status

Array
(
    [1] => 1
    [2] => 0
    [3] => 0
    [4] => 4
    [5] => 4
)

$color_code_string = implode(",",$status);

Ouput

1,0,0,4,4

$color_code_string = str_replace("0","'#F00'",$color_code_string); 
$color_code_string = str_replace("1","'#00bcd4'",$color_code_string);
$color_code_string = str_replace("2","'#4caf50'",$color_code_string);
$color_code_string = str_replace("3","'#bdbdbd'",$color_code_string);
$color_code_string = str_replace("4","'#ff9900'",$color_code_string);

Exception

SyntaxError: illegal character
colors: ['#00bcd'#ff9900'','#F00','#F00','#ff9900','#ff9900']

//prints '#00bcd'#ff9900'','#F00','#F00','#ff9900','#ff9900'

How do I achieve expected output as below

'#00bcd','#ff9900','#F00','#F00','#ff9900','#ff9900'
Slimshadddyyy
  • 4,085
  • 5
  • 59
  • 121

4 Answers4

5

That happens because you are also replacing numbers inside the color codes you replaced before. Solution: traverse the array to do the replacement before imploding the array of colors:

// Translation table, saves you separate lines of stringreplace calls.
$colorCodes = array(
  0 => "#F00",
  1 => "#00bcd4",
  2 => "#4caf50",
  3 => "#bdbdbd",
  4 => "#ff9900",
);

// Build an array of colors based on the array of status codes and the translation table.
// I'm adding the quotes here too, but that's up to you.
$statusColors = array();
foreach($status as $colorCode) {
  $statusColors[] = "'{$colorCodes[$colorCode]}'";
} 

// Last step: implode the array of colors.
$colors = implode(','$statusColors);
GolezTrol
  • 114,394
  • 18
  • 182
  • 210
2
$status = [1,0,0,4,4,];
$color_code_string = implode(",",$status);
$replacements = ["0" => "'#F00'","1" => "'#00bcd4'","2" => "'#4caf50'","3" => "'#bdbdbd'","4" => "'#ff9900'",];
$color_code_string = strtr($color_code_string, $replacements); 
echo $color_code_string;
Amit
  • 3,251
  • 3
  • 20
  • 31
1

There is a big Caution notice about your problem in the str_replace() documentation:

Caution Replacement order gotcha

Because str_replace() replaces left to right, it might replace a previously inserted value when doing multiple replacements. See also the examples in this document. Use strtr() instead, because str_replace() will overwrite previous replacements

$status = [
    1,
    0,
    0,
    4,
    4,
];

$color_code_string = implode(",",$status);

$replacements = [
    "0" => "'#F00'",
    "1" => "'#00bcd4'",
    "2" => "'#4caf50'",
    "3" => "'#bdbdbd'",
    "4" => "'#ff9900'",
];


$color_code_string = strtr($color_code_string, $replacements); 
echo $color_code_string;
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
0
<?php
$color_code = array(1, 0, 0, 4, 4);

array_walk($color_code, 'color_code_replace');
$color_code_string = implode(",",$color_code);

function color_code_replace(&$cell) {
    switch ($cell) {
        case 0 : {
            $cell = '#F00';
            break;
        }
        case 1 : {
            $cell = '#00bcd4';
            break;
        }
        case 2 : {
            $cell = '#4caf50';
            break;
        }
        case 3 : {
            $cell = '#bdbdbd';
            break;
        }
        case 4 : {
            $cell = '#ff9900';
            break;
        }
        default : {
            throw new Exception("Unhandled Color Code");
        }
    }
}

var_dump($color_code);
Halayem Anis
  • 7,654
  • 2
  • 25
  • 45