PHP

WAYS TO CONVERT ARRAY TO STRING IN PHP

THE ARRAY $thearray = ["red", "green", "blue"];   IMPLODE ARRAY $thestring = implode(", ", $thearray); echo $thestring; red, green, blue

IMPLODE ARRAY

01

REDUCTION FUNCTION function reduce ($carry, $i) {   if ($i !="Green") { $carry .= "$i, ";  }    return $carry; }

ARRAY REDUCE

02

REDUCE ARRAY $arr = ["red", "green", "blue"]; $str = array_reduce($arr, "reduce"); $str = substr($str, 0, -2); echo $str; red, blue

THE ARRAY $thearray = ["red", "green", "blue"];   MANUAL LOOP & COMBINE $thestring = ""; foreach ($thearray as $item) {   $thestring .= "$item, "; } $thestring = substr($thestring, 0, -2); echo $thestring; red, green, blue

MANUAL LOOP

03

THE ARRAY $thearray = ["red", "green", "blue"]; JSON ENCODE $thestring = json_encode($thearray); echo $thestring; ["red","green","blue"]

JSON ENCODE

04

THE ARRAY $thearray = ["red", "green", "blue"]; PHP SERIALIZE $thestring = serialize($thearray); echo $thestring; a:4:{i:0;s:3:"red";i:1;s:5:"green" ;i:2;s:4:"blue"}

SERIALIZE DATA

05