{"id":3633,"date":"2021-02-11T17:05:56","date_gmt":"2021-02-11T17:05:56","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=3633"},"modified":"2026-04-07T12:41:25","modified_gmt":"2026-04-07T11:41:25","slug":"comma-separated-strings","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/php\/strings\/comma-separated-strings","title":{"rendered":"Comma Separated Strings"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Convert comma separated string to array and test for entries<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">Ensure no trailing comma that will produce a blank final array entry<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>  $MyCommaSeperatedString = \"abc,def,ghi,\";\n\n  \/\/Ensure there is no trailing comma\n  $MyCommaSeperatedString = rtrim(trim($MyCommaSeperatedString), \",\");\n  $MyArray = ($MyCommaSeperatedString === '') ? &#91;] : explode(',', $MyCommaSeperatedString);\n  \/\/(No commas will result in the text in the string being in an array of length 1, an empty string will result in an empty array)\n\n  print_r($MyArray);\n\n  \/\/$MyCommaSeperatedString = \"abc,def,ghi,\";\n  \/\/Will output: Array ( &#91;0] => abc &#91;1] => def &#91;2] => ghi )\n\n  \/\/$MyCommaSeperatedString = \"\";\n  \/\/Will output: Array ()<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Examples<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>  $MyCommaSeperatedString = \"abc,def,ghi\";\n  $MyArray = ($MyCommaSeperatedString === '') ? &#91;] : explode(',', $MyCommaSeperatedString);\n  print_r($MyArray);\n  \/\/Will output: Array ( &#91;0] =&gt; abc &#91;1] =&gt; def &#91;2] =&gt; ghi ) \n    \n\n  $MyCommaSeperatedString = \"abc,def,ghi,\";\n  $MyArray = ($MyCommaSeperatedString === '') ? &#91;] : explode(',', $MyCommaSeperatedString);\n  print_r($MyArray);\n  \/\/Will output: Array ( &#91;0] =&gt; abc &#91;1] =&gt; def &#91;2] =&gt; ghi &#91;3] =&gt; )\n    \n\n  $MyCommaSeperatedString = rtrim(trim(\"abc,def,ghi,\"), \",\");      \/\/Ensure there is no trailing comma\n  $MyArray = ($MyCommaSeperatedString === '') ? &#91;] : explode(',', $MyCommaSeperatedString);\n  print_r($MyArray);\n  \/\/Will output: Array ( &#91;0] =&gt; abc &#91;1] =&gt; def &#91;2] =&gt; ghi )<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Convert array into comma separated string<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>    $MyArray = array();\n    $MyArray&#91;] = 'abc';\n    $MyArray&#91;] = 'def';\n    $MyArray&#91;] = 'ghi';\n    $MyCommaSeperatedString = implode(',', $MyArray);      \/\/Convert array to comma seperated string\n    echo $MyCommaSeperatedString . \"&lt;br&gt;\";\n    \/\/Will output: abc,def,ghi\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Add new value to a comma separated string<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>    if ($MyCommaSeperatedString  === '')\n      $MyCommaSeperatedString  = $MyNewValue;\n    else\n      $MyCommaSeperatedString  .= ',' . $MyNewValue;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Convert comma separated string to array and test for entries Ensure no trailing comma that will produce a blank final array entry Examples Convert array into comma separated string Add new value to a comma separated string<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31,19],"tags":[],"class_list":["post-3633","post","type-post","status-publish","format-standard","hentry","category-arrays","category-strings"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3633","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=3633"}],"version-history":[{"count":9,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3633\/revisions"}],"predecessor-version":[{"id":5344,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3633\/revisions\/5344"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=3633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=3633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=3633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}