{"id":3134,"date":"2020-08-18T09:03:20","date_gmt":"2020-08-18T08:03:20","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=3134"},"modified":"2022-02-17T07:13:47","modified_gmt":"2022-02-17T07:13:47","slug":"multidimensional-arrays","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/php\/memory\/arrays\/multidimensional-arrays","title":{"rendered":"Multidimensional arrays"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Two Dimension Arrays<\/h4>\n\n\n\n<p>PHP arrays can contain a different array in any index, so you can create arrays as nested as you like!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  $MyArray = array(\n               array('1', '2', '3', '4'),\n               array('10', '20', '30', '40'),\n               array('100', '200', '300', '400'));\n\n  echo $MyArray&#91;1]&#91;2];       \/\/Displays 30\n\n\/\/An alternative more minimal notation way\n  $MyArray = &#91;\n               &#91;'1', '2', '3', '4'],\n               &#91;'10', '20', '30', '40'],\n               &#91;'100', '200', '300', '400']\n             ];<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\t\t$MyArray = array(\n\t\t\t\t\t\t$array = array_fill(0, 24, false),\t\t\/\/Fill an array from index 0, 24 items long, with default value false\n\t\t\t\t\t\t$array = array_fill(0, 24, false),\n\t\t\t\t\t\t$array = array_fill(0, 24, false),\n\t\t\t\t\t\t);<\/code><\/pre>\n\n\n\n<p>You can carry on creating arrays within arrays if desired.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Accessing Array Values<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\t$my_val = $my_array&#91;0]&#91;video_index];<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Creating a multidimensional array<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">Example 1<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>  $MyMultiDimArray = array();\n  $MyMultiDimArray&#91;] = array(\n    \"id\" =&gt;  14,\n    \"name\" =&gt; \"Adam\"\n  );<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Example 2<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>  $MyMultiDimArray = array(    \n    0 =&gt;  array(  \n      \"id\" =&gt;  14,\n      \"name\" =&gt; \"Adam\",\n    ),  \n    1   =&gt;  array(  \n        \"id\" =&gt; 2032,\n        \"name\" =&gt; \"James\"\n    ), \n    2   =&gt;  array(  \n        \"id\" =&gt; 18,\n        \"name\" =&gt; \"Jane\"\n    )\n  );<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Example 3<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>  $MyMultiDimArray = array();\n        \n  if (!isset($MyMultiDimArray&#91;'DataAvailability']))\n    $MyMultiDimArray&#91;'DataAvailability'] = array();\n        \n  if (!isset($Output&#91;'DataAvailability']&#91;'DeviceId']))\n    $Output&#91;'DataAvailability']&#91;'DeviceId'] = array();<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Does a field value exist within a multi dimensional array?<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>  $MyMultiDimArray = array(    \n    0 =&gt;  array(  \n      \"id\" =&gt;  14,\n      \"name\" =&gt; \"Adam\",\n    ),  \n    1   =&gt;  array(  \n        \"id\" =&gt; 2032,\n        \"name\" =&gt; \"James\"\n    ), \n    2   =&gt;  array(  \n        \"id\" =&gt; 18,\n        \"name\" =&gt; \"Jane\"\n    )\n  );\n\n\n  \/\/Simple exists or not\n  if (array_search(2032, array_column($MyMultiDimArray, 'id')) !== False)\n    echo 'FOUND!';\n  else\n    echo 'NOT FOUND!';\n\n\n  \/\/Get key if it exists - looking for match\n  $Key = array_search(2032, array_column($MyMultiDimArray, 'id'));\n  if ($Key !== False)\n  {\n    $Name = $MyMultiDimArray&#91;$Key]&#91;'name'];\n  }\n\n\n  \/\/Get key if it exists  - create if not exist\n  $Key = array_search(2032, array_column($MyMultiDimArray, 'id'));\n  if ($Key === False)\n  {\n    $MyMultiDimArray&#91;]&#91;'id'] = 2032;\n    $Key = count($MyMultiDimArray) - 1;\n  }<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Two Dimension Arrays PHP arrays can contain a different array in any index, so you can create arrays as nested as you like! You can carry on creating arrays within arrays if desired. Accessing Array Values Creating a multidimensional array Example 1 Example 2 Example 3 Does a field value exist within a multi dimensional [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31],"tags":[],"class_list":["post-3134","post","type-post","status-publish","format-standard","hentry","category-arrays"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3134","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=3134"}],"version-history":[{"count":8,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3134\/revisions"}],"predecessor-version":[{"id":3923,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/3134\/revisions\/3923"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=3134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=3134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=3134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}