{"id":4,"date":"2003-08-09T20:05:22","date_gmt":"2003-08-09T19:05:22","guid":{"rendered":"http:\/\/coding.mu\/index.php\/archives\/2003\/08\/09\/php-collection-class\/"},"modified":"2025-11-06T18:39:27","modified_gmt":"2025-11-06T18:39:27","slug":"php-collection-class","status":"publish","type":"post","link":"https:\/\/priscimon.net\/coding\/2003\/08\/09\/php-collection-class\/","title":{"rendered":"PHP collection class"},"content":{"rendered":"\n<p>This is a simple PHP class that can be used to manage a collection of items.<\/p>\n\n\n\n<p>The usage is shown at the end.<\/p>\n\n\n\n<h6 class=\"wp-block-heading\">Collection.php:<\/h6>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?php\nclass Collection {\n    var $elements = array();\n    var $counter = 0;\n    var $pointer = 0;\n\n    function Collection() {\n\n    }\n\n    function add($element) {\n        $this-&gt;elements[$this-&gt;counter] = $element;\n        $this-&gt;counter++;\n        $this-&gt;pointer++;\n    }\n\n    function remove($element) {\n        $found = null;\n        for ($i = 0; $i &lt; count($this-&gt;elements); $i++) {\n            if ($this-&gt;elements[$i] == $element) {\n                $found = $i;\n            }\n        }\n        if ($found != null) {\n            array_splice($this-&gt;elements, $found, 1);\n            $this-&gt;counter--;\n            $this-&gt;pointer--;\n        }\n    }\n\n    function contains($element) {\n        for ($i = 0; $i &lt; count($this-&gt;elements); $i++) {\n            if ($this-&gt;elements[$i] == $element) {\n                return true;\n            }\n        }\n        return false;\n    }\n\n    function hasNext() {\n        return $this-&gt;pointer &lt; $this-&gt;counter;\n    }\n\n    function hasPrevious() {\n        return $this-&gt;pointer &gt; 0;\n    }\n\n    function next() {\n        return $this-&gt;elements[$this-&gt;pointer++];\n    }\n\n    function first() {\n        $this-&gt;pointer = 0;\n        return $this-&gt;elements[$this-&gt;pointer];\n    }\n\n    function last() {\n        $this-&gt;pointer = $this-&gt;counter;\n        return $this-&gt;elements[$this-&gt;pointer];\n    }\n\n    function previous() {\n        return $this-&gt;elements[--$this-&gt;pointer];\n    }\n\n    function count() {\n        return count($this-&gt;elements);\n    }\n}\n?&gt;<\/pre>\n\n\n\n<h6 class=\"wp-block-heading\">Usage:<\/h6>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?php<br \/>require_once \"Collection.php\";<br \/><br \/>$collection = new Collection();<br \/><br \/>for ($i = 0; $i &lt; 10; $i++) {<br \/>    $element = \"Element $i\";<br \/>    $collection->add($element);<br \/>}<br \/><br \/>$collection->first();<br \/>while ($collection->hasNext() ) {<br \/>    $element = $collection->next();<br \/>    echo $element . \"n\";<br \/>}<br \/><br \/>echo \"\\n\";<br \/><br \/>$collection->previous();<br \/>$collection->previous();<br \/>$collection->remove(\"Element 7\");<br \/><br \/>$collection->last();<br \/>while ($collection->hasPrevious() ) {<br \/>    $element = $collection->previous();<br \/>    echo $element . \"n\";<br \/>}<br \/><br \/>?><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is a simple PHP class that can be used to manage a collection of items. The usage is shown at the end. Collection.php: &lt;?php class Collection { var $elements = array(); var $counter = 0; var $pointer = 0; function Collection() { } function add($element) { $this-&gt;elements[$this-&gt;counter] = $element; $this-&gt;counter++; $this-&gt;pointer++; } function remove($element) [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_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":[1],"tags":[],"class_list":["post-4","post","type-post","status-publish","format-standard","hentry","category-general"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3I4g9-4","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/posts\/4","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/comments?post=4"}],"version-history":[{"count":10,"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/posts\/4\/revisions"}],"predecessor-version":[{"id":2016,"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/posts\/4\/revisions\/2016"}],"wp:attachment":[{"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/media?parent=4"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/categories?post=4"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/priscimon.net\/coding\/wp-json\/wp\/v2\/tags?post=4"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}