{"id":209,"date":"2016-07-28T03:57:16","date_gmt":"2016-07-28T03:57:16","guid":{"rendered":"https:\/\/barkhane.com\/php\/2016\/07\/28\/stack-php\/"},"modified":"2024-03-03T15:03:32","modified_gmt":"2024-03-03T15:03:32","slug":"stack-php","status":"publish","type":"post","link":"https:\/\/barkhane.com\/php\/stack-php\/","title":{"rendered":"Stack PHP"},"content":{"rendered":"<h2>How to use Stack \u00a0in PHP array ?<\/h2>\n<p>Stack implementation in <a href=\"https:\/\/barkhane.com\/php\/what-is-php\/\" target=\"_blank\" rel=\"noopener\">PHP<\/a>\u00a0<a href=\"https:\/\/barkhane.com\/php\/what-are-arrays-in-php\/\" target=\"_blank\" rel=\"noopener\">array<\/a>,We know that stack is the method of adding on top and deletion also on the top ,that mean the element add first will be deleted in last,in php Stack implementation \u00a0can be done in two ways<\/p>\n<ul>\n<li>push <a href=\"https:\/\/barkhane.com\/php\/what-are-arrays-in-php\/\" target=\"_blank\" rel=\"noopener\">array<\/a><\/li>\n<li>pop <a href=\"https:\/\/barkhane.com\/php\/what-are-arrays-in-php\/\" target=\"_blank\" rel=\"noopener\">array<\/a><\/li>\n<\/ul>\n<p>To understand push and pop we take an <a href=\"https:\/\/barkhane.com\/php\/what-are-arrays-in-php\/\" target=\"_blank\" rel=\"noopener\">array<\/a>\u00a0$month=array(\\&#8217;jan\\&#8217;,\\&#8217;feb\\&#8217;), initially added to element in it.(<strong>$month<\/strong> is an\u00a0<a href=\"https:\/\/barkhane.com\/php\/what-are-arrays-in-php\/\" target=\"_blank\" rel=\"noopener\">array<\/a>\u00a0type variable.)<\/p>\n<p><strong>Push array :<\/strong> By this method we are adding element in array using array function array_push().now we have add a month \\&#8217;mar\\&#8217; to do that we use \u00a0array_push() function in which we provide two parameter first is array\u00a0name and second value .<\/p>\n<pre><code>\narray_push($month,\\'mar\\');\nprint_r($month);\n<\/code><\/pre>\n<p>print_r will return formatted output<br \/>\nThis will add mar in $month array on 3rd index,as\u00a0array_push() work on stack function therefore the element will add at the end of the array.<\/p>\n<p><strong>Pop\u00a0array :<\/strong> By this method we are deleting\u00a0element from <a href=\"https:\/\/barkhane.com\/php\/what-are-arrays-in-php\/\" target=\"_blank\" rel=\"noopener\">array<\/a>\u00a0using array function array_pop().<\/p>\n<pre><code>\narray_pop($month);\nprint_r($month);\n<\/code><\/pre>\n<p>print_r will return formatted output<\/p>\n<p>The array_pop() function deletes the last element of an <a href=\"https:\/\/barkhane.com\/php\/what-are-arrays-in-php\/\" target=\"_blank\" rel=\"noopener\">array<\/a>\u00a0.as function\u00a0work on stack function therefore the element will deleted\u00a0at the end of the array. ie. the newest element will be deleted. In above example the mar will be deleted after using array_pop() from the <a href=\"https:\/\/barkhane.com\/php\/what-are-arrays-in-php\/\" target=\"_blank\" rel=\"noopener\">array<\/a>.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to use Stack \u00a0in PHP array ? Stack implementation in PHP\u00a0array,We know that stack is the method of adding [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[3],"tags":[],"class_list":["post-209","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts\/209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/comments?post=209"}],"version-history":[{"count":0,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/posts\/209\/revisions"}],"wp:attachment":[{"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/media?parent=209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/categories?post=209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/barkhane.com\/php\/wp-json\/wp\/v2\/tags?post=209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}