{"id":2106,"date":"2013-01-27T09:50:17","date_gmt":"2013-01-27T09:50:17","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=2106"},"modified":"2013-01-27T09:50:17","modified_gmt":"2013-01-27T09:50:17","slug":"php-add-to-an-array-add-element-to-array-using-php-examples","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/php-add-to-an-array-add-element-to-array-using-php-examples\/","title":{"rendered":"Add To An Array Using PHP Examples"},"content":{"rendered":"<p>No programmer or developer can deny the importance and usefulness of arrays, regardless of the programming language used. A very basic, yet important concept is to <strong>add element to array using PHP<\/strong>. So do you know how to add an item to an empty array or simply add to an array with pre-existing elements? If not, then this article is for you. In this example, I am going to show\u00a0how to use <em><strong>PHP to add\u00a0to an array<\/strong><\/em>.<\/p>\n<h2>How to use PHP to Add An Element to An Array<\/h2>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=2106\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<p>Let&#8217;s look at <em><strong>how to add item to array<\/strong><\/em> by means of an example. For this, we will start by creating an empty array. And then we will see step-by-step, how to add individual items to the array. Once we are done adding elements to array using PHP, we will output the resultant array just to make sure that we did everything right. So here it goes:<\/p>\n<pre class=\"lang:php decode:true\" title=\"Example\">&lt;?php \n\/\/Declare an empty array\n$my_fruits_array = array();\n\n\/\/Add an item to the array\n$my_fruits_array[] = 'apple';\n\n\/\/Add second item to the array\n$my_fruits_array[] = 'mango';\n\n\/\/Add third item to the array\n$my_fruits_array[] = 'grapes';\n\n\/\/Print the contents of the array. Use the &lt;pre&gt; tags to output the array in readable format.\necho '&lt;pre&gt;';\nprint_r($my_fruits_array);\necho '&lt;\/pre&gt;';\n?&gt;<\/pre>\n<p>As you can see from the above code, we are adding items to the array, one-by-one &amp; at the end, we are showing echoing the output after adding a value to an array.<\/p>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=2106\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<p><strong>Your Turn!<\/strong><\/p>\n<p>Do you know of any other ways to <em><strong>PHP to\u00a0<\/strong><strong>add an element to an array<\/strong><\/em>? Feel free to suggest by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>No programmer or developer can deny the importance and usefulness of arrays, regardless of the programming language used. A very basic, yet important concept is to add element to array using PHP. So do you know how to add an item to an empty array or simply add to an array with pre-existing elements? If [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":141,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[10,13],"tags":[33],"class_list":["post-2106","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php-tips-and-tricks","category-tips-and-tricks","tag-php-tips"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paBnQX-xY","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/2106","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/comments?post=2106"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/2106\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/media?parent=2106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=2106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=2106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}