{"id":26031,"date":"2022-12-15T03:14:32","date_gmt":"2022-12-14T21:44:32","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-php-data-strucure-to-hold-matrix-of-strings-and-numeric-values\/"},"modified":"2022-12-15T03:14:32","modified_gmt":"2022-12-14T21:44:32","slug":"solved-php-data-strucure-to-hold-matrix-of-strings-and-numeric-values","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-php-data-strucure-to-hold-matrix-of-strings-and-numeric-values\/","title":{"rendered":"[Solved] PHP &#8211; Data Strucure to hold Matrix of Strings and Numeric Values"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-37024987\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"37024987\" data-parentid=\"37017531\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>I found the answer i was looking for from below link.<br \/>\n[<a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/hawkee.com\/snippet\/10086\/%5D\">http:\/\/hawkee.com\/snippet\/10086\/]<\/a><\/p>\n<p>for anyone who is interested in this finding out i will post it here.<\/p>\n<p>There is second solution for this also using array within array. Which i will post below this, <\/p>\n<pre><code>&lt;?php \nclass Matrix {\n    public $arr, $rows, $cols;\n\n    function Matrix($row, $col) {\n        if ($row &gt; 0 &amp;&amp; $col &gt; 0) {\n            $arr        = array();\n            $this-&gt;rows = $row;\n            $this-&gt;cols = $col;\n\n            for ($a=0; $a &lt; $this-&gt;rows; $a++) {\n                array_push($arr,array());\n\n                for ($b=0; $b &lt; $this-&gt;cols; $b++) {\n                    array_push($arr[$a],0);\n                }\n            }\n        }\n    }\n\n    function SetElem($x, $y, $val) {\n        if ($x &gt; -1 &amp;&amp; $x &lt; $this-&gt;rows) {\n            if ($y &gt; -1 &amp;&amp; $y &lt; $this-&gt;cols) {\n                $this-&gt;arr[$x][$y] = $val;\n            }\n        }\n    }\n\n    function GetElem($x, $y) {\n        if ($x &gt; -1 &amp;&amp; $x &lt; $this-&gt;rows) {\n            if ($y &gt; -1 &amp;&amp; $y &lt; $this-&gt;cols) {\n                return $this-&gt;arr[$x][$y];\n            }\n        }\n    }\n\n    function Add($matrix) {\n        if ($this-&gt;rows == $matrix-&gt;rows &amp;&amp; $this-&gt;cols == $matrix-&gt;cols) {\n            $rslt = new Matrix($this-&gt;rows,$this-&gt;cols);\n\n            for ($a=0; $a &lt; $this-&gt;rows; $a++) {\n                for ($b=0; $b &lt; $this-&gt;cols; $b++) {\n                    $rslt-&gt;SetElem($a,$b,$this-&gt;GetElem($a,$b) + $matrix-&gt;GetElem($a,$b));\n                }\n            }\n\n            return $rslt;\n        }\n    }\n\n    function Subtract($matrix) {\n        if ($this-&gt;rows == $matrix-&gt;rows &amp;&amp; $this-&gt;cols == $matrix-&gt;cols) {\n            $rslt = new Matrix($this-&gt;rows,$this-&gt;cols);\n\n            for ($a=0; $a &lt; $this-&gt;rows; $a++) {\n                for ($b=0; $b &lt; $this-&gt;cols; $b++) {\n                    $rslt-&gt;SetElem($a,$b,$this-&gt;GetElem($a,$b) - $matrix-&gt;GetElem($a,$b));\n                }\n            }\n\n            return $rslt;\n        }\n    }\n\n    function Multiply($matrix) {\n        if ($this-&gt;cols == $matrix-&gt;rows) {\n            $rslt = new Matrix($this-&gt;rows, $matrix-&gt;cols);\n\n            for ($a=0; $a &lt; $rslt-&gt;rows; $a++) {\n                for ($b=0; $b &lt; $rslt-&gt;cols; $b++) {\n                    $total = 0;\n\n                    for ($c=0; $c &lt; $matrix-&gt;rows; $c++) {\n                        $total += $this-&gt;GetElem($a,$c) * $matrix-&gt;GetElem($c,$b);\n                    }\n\n                    $rslt-&gt;SetElem($a,$b,$total);\n                }\n            }\n\n            return $rslt;\n        }\n    }\n\n    function ScalarMultiply($num) {\n        $rslt = new Matrix($this-&gt;rows,$this-&gt;cols);\n\n        for  ($a=0; $a &lt; $rslt-&gt;rows; $a++) {\n            for  ($b=0; $b &lt; $rslt-&gt;cols; $b++) {\n                $rslt-&gt;SetElem($a,$b,$this-&gt;GetElem($a,$b) * $num);\n            }\n        }\n\n        return $rslt;\n    }\n}\n?&gt;\n<\/code><\/pre>\n<p>Usage:<\/p>\n<pre><code>&lt;?php include 'Matrix.php'; \n$mat = new Matrix(2,2);\n\n$mat-&gt;SetElem(0,0,1);\n$mat-&gt;SetElem(0,1,2);\n$mat-&gt;SetElem(1,0,3);\n$mat-&gt;SetElem(1,1,4);\n\n$mat2 = new Matrix(2,2);\n\n$mat2-&gt;SetElem(0,0,5);\n$mat2-&gt;SetElem(0,1,6);\n$mat2-&gt;SetElem(1,0,7);\n$mat2-&gt;SetElem(1,1,8);\n\n$MultiplyResult = $mat-&gt;Multiply($mat2);\n$ScalarResult   = $mat-&gt;ScalarMultiply(2);\n$AddResult      = $mat-&gt;Add($mat2);\n$SubtractResult = $mat-&gt;Subtract($mat2);\n<\/code><\/pre>\n<p>second solution was to not all that good but still will do the work.<\/p>\n<pre><code>$graha = array\n  (\n  array(\"Sun\",5,5,0,4 ... ),\n  array(\"Moon\",5,3,...),\n  ... \n);\n<\/code><\/pre>\n<p>In this you have to keep the index[1=Sun,2=Moon,] somewhere else to find out which element it matches when requesting elements from the matrix.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\"><\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved PHP &#8211; Data Strucure to hold Matrix of Strings and Numeric Values <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] I found the answer i was looking for from below link. [http:\/\/hawkee.com\/snippet\/10086\/] for anyone who is interested in this finding out i will post it here. There is second solution for this also using array within array. Which i will post below this, &lt;?php class Matrix { public $arr, $rows, $cols; function Matrix($row, $col) &#8230; <a title=\"[Solved] PHP &#8211; Data Strucure to hold Matrix of Strings and Numeric Values\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-php-data-strucure-to-hold-matrix-of-strings-and-numeric-values\/\" aria-label=\"More on [Solved] PHP &#8211; Data Strucure to hold Matrix of Strings and Numeric Values\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[859,1331,339],"class_list":["post-26031","post","type-post","status-publish","format-standard","hentry","category-solved","tag-data-structures","tag-matrix","tag-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] PHP - Data Strucure to hold Matrix of Strings and Numeric Values - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-php-data-strucure-to-hold-matrix-of-strings-and-numeric-values\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] PHP - Data Strucure to hold Matrix of Strings and Numeric Values - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] I found the answer i was looking for from below link. [http:\/\/hawkee.com\/snippet\/10086\/] for anyone who is interested in this finding out i will post it here. There is second solution for this also using array within array. Which i will post below this, &lt;?php class Matrix { public $arr, $rows, $cols; function Matrix($row, $col) ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-php-data-strucure-to-hold-matrix-of-strings-and-numeric-values\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-14T21:44:32+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-php-data-strucure-to-hold-matrix-of-strings-and-numeric-values\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-php-data-strucure-to-hold-matrix-of-strings-and-numeric-values\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] PHP &#8211; Data Strucure to hold Matrix of Strings and Numeric Values\",\"datePublished\":\"2022-12-14T21:44:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-php-data-strucure-to-hold-matrix-of-strings-and-numeric-values\/\"},\"wordCount\":114,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"data-structures\",\"matrix\",\"php\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-php-data-strucure-to-hold-matrix-of-strings-and-numeric-values\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-php-data-strucure-to-hold-matrix-of-strings-and-numeric-values\/\",\"name\":\"[Solved] PHP - Data Strucure to hold Matrix of Strings and Numeric Values - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-12-14T21:44:32+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-php-data-strucure-to-hold-matrix-of-strings-and-numeric-values\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-php-data-strucure-to-hold-matrix-of-strings-and-numeric-values\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-php-data-strucure-to-hold-matrix-of-strings-and-numeric-values\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] PHP &#8211; Data Strucure to hold Matrix of Strings and Numeric Values\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] PHP - Data Strucure to hold Matrix of Strings and Numeric Values - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-php-data-strucure-to-hold-matrix-of-strings-and-numeric-values\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] PHP - Data Strucure to hold Matrix of Strings and Numeric Values - JassWeb","og_description":"[ad_1] I found the answer i was looking for from below link. [http:\/\/hawkee.com\/snippet\/10086\/] for anyone who is interested in this finding out i will post it here. There is second solution for this also using array within array. Which i will post below this, &lt;?php class Matrix { public $arr, $rows, $cols; function Matrix($row, $col) ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-php-data-strucure-to-hold-matrix-of-strings-and-numeric-values\/","og_site_name":"JassWeb","article_published_time":"2022-12-14T21:44:32+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-php-data-strucure-to-hold-matrix-of-strings-and-numeric-values\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-php-data-strucure-to-hold-matrix-of-strings-and-numeric-values\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] PHP &#8211; Data Strucure to hold Matrix of Strings and Numeric Values","datePublished":"2022-12-14T21:44:32+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-php-data-strucure-to-hold-matrix-of-strings-and-numeric-values\/"},"wordCount":114,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["data-structures","matrix","php"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-php-data-strucure-to-hold-matrix-of-strings-and-numeric-values\/","url":"https:\/\/jassweb.com\/solved\/solved-php-data-strucure-to-hold-matrix-of-strings-and-numeric-values\/","name":"[Solved] PHP - Data Strucure to hold Matrix of Strings and Numeric Values - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-12-14T21:44:32+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-php-data-strucure-to-hold-matrix-of-strings-and-numeric-values\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-php-data-strucure-to-hold-matrix-of-strings-and-numeric-values\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-php-data-strucure-to-hold-matrix-of-strings-and-numeric-values\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] PHP &#8211; Data Strucure to hold Matrix of Strings and Numeric Values"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/26031","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=26031"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/26031\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=26031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=26031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=26031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}