{"id":1395,"date":"2012-01-18T08:51:18","date_gmt":"2012-01-18T06:51:18","guid":{"rendered":"http:\/\/www.karadere.com\/blog\/?p=1395"},"modified":"2012-01-18T08:51:18","modified_gmt":"2012-01-18T06:51:18","slug":"php-array-ornegi-php-array-example","status":"publish","type":"post","link":"https:\/\/www.karadere.com\/blog\/php-array-ornegi-php-array-example.html","title":{"rendered":"Php Array \u00f6rne\u011fi ? Php array example"},"content":{"rendered":"<div id=\"middle\">Arrays are used in any programming language. You can imagine an array as a long box with many the same compartments, like this |___|___|___|___|___|.What ever you put in a compartment is its value. The following array |_a_|_b_|_c_|_d_|_e_| contains characters a,b,c,d,e. To access a value you should know in which compartment it is placed. For example &#8216;b&#8217; is in the second compartment. In most computer languages array index (counting) starts from 0, not from 1. Index of the first element of the array is 0, Index of the second element of the array is 1 and so on. In array of names below you can see indexes and values.To display &#8220;Anna&#8221; array value you have to access element (compartement) with index 3. print($names[3]); To display all array values &#8211; use for loop or print array function.<\/p>\n<div id=\"code\">\/\/declare an array of names<br \/>\n$names=array();$message=&#8221;Hello &#8220;;<br \/>\n$prefix1=&#8221;Mr.&#8221;;<br \/>\n$prefix2=&#8221;Mrs.&#8221;;$names[0]=&#8221;John&#8221;;<br \/>\n$names[1]=&#8221;George&#8221;;<br \/>\n$names[2]=&#8221;James&#8221;;<br \/>\n$names[3]=&#8221;Anna&#8221;;<br \/>\n$names[4]=&#8221;Robert&#8221;;<br \/>\n$names[5]=&#8221;John&#8221;;<br \/>\n$names[6]=&#8221;James&#8221;;<br \/>\n$names[7]=&#8221;George&#8221;;<br \/>\n$names[8]=&#8221;Maria&#8221;;<br \/>\n$names[9]=&#8221;Peter&#8221;;<br \/>\n$names[10]=&#8221;James&#8221;;<\/p>\n<p>print(&#8216;&lt;br&gt;The sort function sorts array&lt;br&gt;&#8217;);<br \/>\nsort($names);<\/p>\n<p>\/\/Get size of array<br \/>\n$asize=sizeof($names);<\/p>\n<p>for($i=0; $i&lt;$asize; $i++) \u00a0\u00a0\u00a0{<br \/>\n\/\/Check if it is female name put Mrs. prefix<br \/>\n\/\/else put Mr. prefix<\/p>\n<p>if(($names[$i]==&#8221;Anna&#8221;)||($names[$i]==&#8221;Maria&#8221;))<br \/>\n{<br \/>\nprint($message.$prefix2.$names[$i].&#8221;&lt;br&gt;&#8221;);<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nprint($message.$prefix1.$names[$i].&#8221;&lt;br&gt;&#8221;);<br \/>\n}<br \/>\n}<br \/>\nprint(&#8216;&lt;br&gt;&#8217;);<\/p>\n<p>echo &#8220;The array_unique function removes duplicate array values&lt;br&gt;&#8221;;<\/p>\n<p>$array=array();<\/p>\n<p>$array=array_unique($names);<\/p>\n<p>foreach($array as $key =&gt; $value)<br \/>\n{<br \/>\necho $key . &#8220;-&#8220;. $value . &#8220;&lt;br&gt;&#8221;;<br \/>\n}<\/p>\n<p>print(&#8216;&lt;br&gt;&#8217;);<\/p>\n<p>rsort($array);<\/p>\n<p>print(&#8220;The rsort function sorts array in reverse order&lt;br&gt;&#8221;);<\/p>\n<p>foreach($array as $key =&gt; $value)<br \/>\n{<br \/>\necho $key . &#8220;-&#8220;. $value . &#8220;&lt;br&gt;&#8221;;<br \/>\n}<\/p>\n<p>print(&#8216;&lt;br&gt;The array_pop($array) functions returns the last element&lt;br&gt;&#8217;);<\/p>\n<p>$lastelement=array_pop($array);<\/p>\n<p>print(&#8216;&lt;br&gt;The last element=&#8217;.$lastelement.'&lt;br&gt;&#8217;);<\/p>\n<p>print(&#8216;&lt;br&gt;Array after calling array_pop($array): The last element removed&lt;br&gt;&lt;br&gt;&#8217;);<\/p>\n<p>foreach($array as $key =&gt; $value)<br \/>\n{<br \/>\necho $key . &#8220;-&#8220;. $value . &#8220;&lt;br&gt;&#8221;;<br \/>\n}<\/p>\n<p>\/\/The <strong>Array_push<\/strong> function add elements to the end of an array<br \/>\n\/\/The <strong>print_r<\/strong> print array function prints array key &#8211; value pairs<\/p>\n<p>array_push($array, &#8220;Chris&#8221;, &#8220;Colin&#8221;);<\/p>\n<p>print_r($array);<\/p>\n<p>print(&#8216;&lt;br&gt;&lt;br&gt;The array_rand($array) function returns random array index&lt;br&gt;&#8217;);<\/p>\n<p>$random=array_rand($array);<\/p>\n<p>print(&#8216;&lt;br&gt;print array element by random index&lt;br&gt;&#8217;);<\/p>\n<p>print(&#8216;&lt;br&gt;Random element=&#8217;.$array[$random].'&lt;br&gt;&#8217;);<\/p>\n<p>$string=implode($array);<\/p>\n<p>print(&#8220;&lt;br&gt;Array imploded in string:&lt;br&gt;&#8221;);<\/p>\n<p>print($string);<\/p>\n<p>?&gt;<\/p>\n<\/div>\n<p>Autput:<\/p>\n<p>The sort function sorts an array<br \/>\nHello Mrs.Anna<br \/>\nHello Mr.George<br \/>\nHello Mr.George<br \/>\nHello Mr.James<br \/>\nHello Mr.James<br \/>\nHello Mr.James<br \/>\nHello Mr.John<br \/>\nHello Mr.John<br \/>\nHello Mrs.Maria<br \/>\nHello Mr.Peter<br \/>\nHello Mr.Robert<\/p>\n<p>The array_unique function removes duplicate values<br \/>\n0-Anna<br \/>\n1-George<br \/>\n3-James<br \/>\n6-John<br \/>\n8-Maria<br \/>\n9-Peter<br \/>\n10-Robert<\/p>\n<p>The rsort function sorts an array in reverse order<br \/>\n0-Robert<br \/>\n1-Peter<br \/>\n2-Maria<br \/>\n3-John<br \/>\n4-James<br \/>\n5-George<br \/>\n6-Anna<\/p>\n<p>The array_pop($array) functions returns the last element<\/p>\n<p>The last element=Anna<\/p>\n<p>Array after caling array_pop($array): The last element removed<\/p>\n<p>0-Robert<br \/>\n1-Peter<br \/>\n2-Maria<br \/>\n3-John<br \/>\n4-James<br \/>\n5-George<\/p>\n<p>Array after calling array_push($array, &#8220;Chris&#8221;, &#8220;Colin&#8221;)<br \/>\nand print_r: Chris and Colin are added to the end of array<\/p>\n<p>Array ( [0] =&gt; Robert [1] =&gt; Peter [2] =&gt; Maria [3] =&gt; John [4] =&gt; James [5] =&gt; George [6] =&gt; Chris [7] =&gt; Colin )<\/p>\n<p>The array_rand($array) function returns random array index<\/p>\n<p>print array element by random index<\/p>\n<p>Random element=Colin<\/p>\n<p>Array imploded in string:<\/p>\n<p>RobertPeterMariaJohnJamesGeorgeChrisColin<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Arrays are used in any programming language. You can imagine an array as a long box with many the same compartments, like this |___|___|___|___|___|.What ever you put in a compartment is its value. The following array |_a_|_b_|_c_|_d_|_e_| contains characters a,b,c,d,e. To access a value you should know in which compartment it is placed. For example [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":"","_links_to":"","_links_to_target":""},"categories":[41],"tags":[235,517,1128,1333,2165],"class_list":["post-1395","post","type-post","status-publish","format-standard","hentry","category-programlama-software","tag-php","tag-array","tag-how-to","tag-kullanimi","tag-use","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"aioseo_notices":[],"views":3021,"_links":{"self":[{"href":"https:\/\/www.karadere.com\/blog\/wp-json\/wp\/v2\/posts\/1395","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.karadere.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.karadere.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.karadere.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.karadere.com\/blog\/wp-json\/wp\/v2\/comments?post=1395"}],"version-history":[{"count":0,"href":"https:\/\/www.karadere.com\/blog\/wp-json\/wp\/v2\/posts\/1395\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.karadere.com\/blog\/wp-json\/wp\/v2\/media?parent=1395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.karadere.com\/blog\/wp-json\/wp\/v2\/categories?post=1395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.karadere.com\/blog\/wp-json\/wp\/v2\/tags?post=1395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}