{"id":516,"date":"2020-04-15T00:50:00","date_gmt":"2020-04-15T04:50:00","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=516"},"modified":"2021-01-02T00:52:16","modified_gmt":"2021-01-02T05:52:16","slug":"removing-quotes-a-step-by-step-example-of-manipulating-array-data","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/removing-quotes-a-step-by-step-example-of-manipulating-array-data\/","title":{"rendered":"Removing quotes &#8211; a step by step example of manipulating array data"},"content":{"rendered":"\n<p>This article shows some techniques of manipulating data in an array. To keep it simple, let&#8217;s start by looking how we can remove quotes from a variable. To remove a character, we can replace it with nothing using str_replace() function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$a = \"'pineapple'\";\n$b = '\"guava\"';\nprint \"$a $b\\n\";\n$a = str_replace(\"'\",\"\",$a);\n$b = str_replace('\"','',$b);\nprint \"$a $b\\n\";<\/code><\/pre>\n\n\n\n<p>This results in:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>'pineapple' \"guava\"\npineapple guava<\/code><\/pre>\n\n\n\n<p>Consider this code<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function remove_single_quotes(&amp;$value) {\n    $value = str_replace(\"'\",\"\",$value);\n} \nfunction strip_single_quotes($value) {\n    return str_replace(\"'\",\"\",$value);\n}\nprint \"original\\n\";\n$fruits = array(\"'apple'\",\"'banana'\",\"'nectar'\");\nprint_r($fruits);\n\n# does not work\nprint \"Using foreach\\n\";\nforeach ($fruits as $fruit) { $fruit = str_replace(\"'\",\"\",$fruit); }\nprint_r($fruits);\n\nprint \"original\\n\";\n$fruits = array(\"'apple'\",\"'banana'\",\"'nectar'\");\nprint_r($fruits);\n\nprint \"Using for loop\\n\";\nfor ($i = 0; $i &lt; count($fruits); $i++) {\n    $fruits&#91;$i] = str_replace(\"'\",\"\",$fruits&#91;$i]);\n}\nprint_r($fruits);\n\nprint \"original\\n\";\n$fruits = array(\"'apple'\",\"'banana'\",\"'nectar'\");\nprint_r($fruits);\n\nprint \"Using for loop by value\\n\";\nfor ($i = 0; $i &lt; count($fruits); $i++) {\n    $fruits&#91;$i] = strip_single_quotes($fruits&#91;$i]);\n}\nprint_r($fruits);\n\nprint \"original\\n\";\n$fruits = array(\"'apple'\",\"'banana'\",\"'nectar'\");\nprint_r($fruits);\n\nprint \"Using for loop by reference\\n\";\nfor ($i = 0; $i &lt; count($fruits); $i++) {\n    remove_single_quotes($fruits&#91;$i]);\n}\nprint_r($fruits);\n\nprint \"original\\n\";\n$fruits = array(\"'apple'\",\"'banana'\",\"'nectar'\");\nprint_r($fruits);\n\nprint \"Using array_walk\\n\";\narray_walk($fruits,'remove_single_quotes');\nprint_r($fruits);<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>original\nArray\n(\n   &#91;0] =&gt; 'apple'\n   &#91;1] =&gt; 'banana'\n   &#91;2] =&gt; 'nectar'\n)\nUsing foreach\nArray\n(\n   &#91;0] =&gt; 'apple'\n   &#91;1] =&gt; 'banana'\n   &#91;2] =&gt; 'nectar'\n)\noriginal\nArray \n(\n   &#91;0] =&gt; 'apple'\n   &#91;1] =&gt; 'banana'\n   &#91;2] =&gt; 'nectar'\n) \nUsing for loop\nArray\n(\n   &#91;0] =&gt; apple\n   &#91;1] =&gt; banana\n   &#91;2] =&gt; nectar\n)\noriginal\nArray\n(\n   &#91;0] =&gt; 'apple'\n   &#91;1] =&gt; 'banana'\n   &#91;2] =&gt; 'nectar'\n)\nUsing for loop by value\nArray\n(\n   &#91;0] =&gt; apple\n   &#91;1] =&gt; banana\n   &#91;2] =&gt; nectar\n)\noriginal\nArray\n(\n   &#91;0] =&gt; 'apple'\n   &#91;1] =&gt; 'banana'\n   &#91;2] =&gt; 'nectar'\n)\nUsing for loop by reference\nArray\n(\n   &#91;0] =&gt; apple\n   &#91;1] =&gt; banana\n   &#91;2] =&gt; nectar\n)\noriginal\nArray\n(\n   &#91;0] =&gt; 'apple'\n   &#91;1] =&gt; 'banana'\n   &#91;2] =&gt; 'nectar'\n)\nUsing array_walk\nArray\n(\n   &#91;0] =&gt; apple\n   &#91;1] =&gt; banana\n   &#91;2] =&gt; nectar\n)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This article shows some techniques of manipulating data in an array. To keep it simple, let&#8217;s start by looking how we can remove quotes from a variable. To remove a character, we can replace it with nothing using str_replace() function. This results in: Consider this code Output:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[24],"class_list":["post-516","post","type-post","status-publish","format-standard","hentry","category-php","tag-php"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/516","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=516"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/516\/revisions"}],"predecessor-version":[{"id":517,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/516\/revisions\/517"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=516"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=516"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=516"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}