{"id":873,"date":"2022-08-30T15:08:36","date_gmt":"2022-08-30T15:08:36","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/need-help-with-php-array-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:08:36","modified_gmt":"2022-08-30T15:08:36","slug":"need-help-with-php-array-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/need-help-with-php-array-collection-of-common-programming-errors\/","title":{"rendered":"Need help with PHP array-Collection of common programming errors"},"content":{"rendered":"<p>Can anybody help me out, I&#8217;m stuck, don&#8217;t know how to write a php nested for loop to convert into a key value array.<\/p>\n<p>This is the array structure.<\/p>\n<p>Needed to turn into key value array( combining JobDescription and userdetail array together)<\/p>\n<pre><code>array(2) {\n    [\"jobDescription\"]=&gt; array(5) {\n        [\"funeralFor\"]=&gt; string(6) \"Myself\"\n        [\"serviceType\"]=&gt; string(1) \"1\"\n        [\"religionType\"]=&gt; string(1) \"2\"\n        [\"area\"]=&gt; string(4) \"2154\"\n        [\"customerComment\"]=&gt; string(6) \"fdfddf\"\n    }\n    [\"userDetail\"]=&gt; array(6) {\n        [\"contactEmail\"]=&gt; string(16) \"fdddf@fffgfg.com\"\n        [\"contactFirstName\"]=&gt; string(6) \"fddfdf\"\n        [\"contactLastName\"]=&gt; string(6) \"fddffd\"\n        [\"contactPhoneNumber\"]=&gt; string(10) \"0420988191\"\n        [\"signup\"]=&gt; array(2) {\n            [\"id\"]=&gt; string(32) \"8048f0f7106c336e1a8825d1d3bec902\"\n            [\"input\"]=&gt; string(3) \"k5m\"\n        }\n        [\"agreement\"]=&gt; string(1) \"1\"\n    }\n} \n<\/code><\/pre>\n<p>Thanks so much in advance<\/p>\n<ol>\n<li>\n<p>You have two arrays stored in an array. You want the values of both arrays under one array instead of two subarrays?<\/p>\n<pre><code>$newArray = array_merge($array['jobDescription'], $array['userDetail']);\n<\/code><\/pre>\n<\/li>\n<li>\n<p>I think you&#8217;re looking for <code>array_merge<\/code>, which merges two arrays together:<\/p>\n<pre><code>$new_arr = array_merge($arr['jobDescription'], $arr['userDetail']);\n<\/code><\/pre>\n<\/li>\n<li>\n<pre><code>array_merge($bigArray['jobDescription'], $bigArray['userDetail']);\n<\/code><\/pre>\n<\/li>\n<li>\n<p>You only need to loop once. No need for nesting. This solution covers where you have an undefined number of arrays to combine. Otherwise, you can use <code>array_merge<\/code> as suggested by Dickie<\/p>\n<pre><code>$allValues = array();\n\nif(count($mainArray) &gt; 0)\n{\n    foreach($mainArray as $arr)\n    {\n        $allValues += $arr;\n    }\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<p>The <code>array_merge()<\/code> function can create an array from multiple arrays. In your example, it works this way:<\/p>\n<pre><code>$yours = array(...);\n$values = array_merge($yours[\"jobDescription\"], $yours[\"userDetail\"]);\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 22:50:43. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Can anybody help me out, I&#8217;m stuck, don&#8217;t know how to write a php nested for loop to convert into a key value array. This is the array structure. Needed to turn into key value array( combining JobDescription and userdetail array together) array(2) { [&#8220;jobDescription&#8221;]=&gt; array(5) { [&#8220;funeralFor&#8221;]=&gt; string(6) &#8220;Myself&#8221; [&#8220;serviceType&#8221;]=&gt; string(1) &#8220;1&#8221; [&#8220;religionType&#8221;]=&gt; string(1) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-873","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/873","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=873"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/873\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=873"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}