{"id":502,"date":"2020-06-10T00:26:00","date_gmt":"2020-06-10T04:26:00","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=502"},"modified":"2021-01-02T00:30:14","modified_gmt":"2021-01-02T05:30:14","slug":"php-array-functions","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/php-array-functions\/","title":{"rendered":"PHP: Array functions"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Passing arrays by value and reference<\/h3>\n\n\n\n<p>The difference between passing by value and passing by reference is that you when pass by value, you are passing a copy of the array. When passing by reference, you are passing a pointer i.e any changes made in the function you pass to can modify the original.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function foo($var) {\n  $var&#91;1]++;\n}\nfunction bar(&amp;$var) {\n  $var&#91;1]++;\n}\n\n$a=array(5,6,7);\nfoo($a);\nprint \"Passed by value:\\n\";\nprint_r($a);\nbar($a);\nprint \"Passed by reference:\\n\";\nprint_r($a);<\/code><\/pre>\n\n\n\n<p><strong>output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Passed by value:\nArray\n(\n  &#91;0] => 5\n  &#91;1] => 6\n  &#91;2] => 7\n)\nPassed by reference:\nArray\n(\n  &#91;0] => 5\n  &#91;1] => 7\n  &#91;2] => 7\n)<\/code><\/pre>\n\n\n\n<p>When passed by value, elements of $a remain unchanged. When passed by reference, the second element of $a changed. The only difference in code is the &amp; in the function parameter.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Removing duplicates values from an array<\/h3>\n\n\n\n<p>The array_unique function removes duplicates from an array.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$aSample = array(1,1,6,6,6,8,\"8\");\nprint_r(array_unique($aSample));<\/code><\/pre>\n\n\n\n<p><strong>output<\/strong><\/p>\n\n\n\n<p>Array ( [0] =&gt; 1 [2] =&gt; 6 [5] =&gt; 8 )<\/p>\n\n\n\n<p>By default all values are compared as strings, thus there is no difference between 8 and &#8220;8&#8221;.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">print_r into a variable<\/h3>\n\n\n\n<p>print_r() prints array contents in a user-friendly format. The output of print_r() can also be stored into a variable. Just remember to pass your array into print_r() with a true flag as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$a = array('pass','print_r','into','variable');\n$variable = print_r($a,true);\nprint $variable;<\/code><\/pre>\n\n\n\n<p><strong>output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Array\n(\n  &#91;0] => pass\n  &#91;1] => print_r\n  &#91;2] => into\n  &#91;3] => variable\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Removing a element from an array<\/h3>\n\n\n\n<p>To remove an element from an array, we use array_splice().<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$arr = array(\"red\", \"green\", \"blue\", \"black\");\nprint_r($arr);\nfor ($i = 0; $i &lt; count($arr); $i++) {\n  if ($i == 2) {\n  array_splice($arr, $i, 1);\n  }\n}\nprint_r($arr);<\/code><\/pre>\n\n\n\n<p>The element blue has been removed from the array.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Array\n(\n  &#91;0] => red\n  &#91;1] => green\n  &#91;2] => blue\n  &#91;3] => black\n)\nArray\n(\n  &#91;0] => red\n  &#91;1] => green\n  &#91;2] => black\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Push and Pop<\/h3>\n\n\n\n<p>array_push() and array_pop() functions are used to push and pop elements on a array. These functions have the following syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>array_push($array,$element);\n$element = array_pop($array);<\/code><\/pre>\n\n\n\n<p>Lets look at the following code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$mya = array(0 => \"zero\",1 => \"one\",2 => \"two\");\narray_push($mya, \"three\");\n$mya&#91;] = \"four\";\nprint_r($mya);<\/code><\/pre>\n\n\n\n<p>This program would produce the following results. array_push() adds an element at the end of the array. The code on the last line is equivalent to a pop.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Array\n(\n  &#91;0] => zero\n  &#91;1] => one\n  &#91;2] => two\n  &#91;3] => three\n  &#91;4] => four\n)<\/code><\/pre>\n\n\n\n<p>array_pop removes the last element from an array. Let&#8217;s look at the following program:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$mya = array(0 => \"zero\",1 => \"one\",2 => \"two\");\n$n = array_pop($mya);\nprint \"$n\\n\";\nprint_r($mya);<\/code><\/pre>\n\n\n\n<p>This program produces the following result.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>two\nArray\n(\n  &#91;0] =&gt; zero\n  &#91;1] =&gt; one\n)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Passing arrays by value and reference The difference between passing by value and passing by reference is that you when pass by value, you are passing a copy of the array. When passing by reference, you are passing a pointer i.e any changes made in the function you pass to can modify the original. output [&hellip;]<\/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-502","post","type-post","status-publish","format-standard","hentry","category-php","tag-php"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/502","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=502"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/502\/revisions"}],"predecessor-version":[{"id":503,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/502\/revisions\/503"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=502"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=502"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=502"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}