{"id":1856,"date":"2017-03-23T16:47:21","date_gmt":"2017-03-23T11:17:21","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=1856"},"modified":"2017-03-28T18:09:15","modified_gmt":"2017-03-28T12:39:15","slug":"insert-new-item-array-position-php","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/insert-new-item-array-position-php\/","title":{"rendered":"[ Solved &#8211; 10 Answers ] PHP &#8211; Insert new item in array on any position in PHP"},"content":{"rendered":"<p><label class=\"label label-Warning\">PROBLEM :<\/label><\/p>\n<p>How can we insert a new item into an array on any position, say for example in the middle of array?<\/p>\n<p>or<\/p>\n<p>How can we insert a new item into an array on any position?<\/p>\n<p>php arrays insert<\/p>\n<p><label class=\"label label-info\">SOLUTION 1:<\/label><\/p>\n<h4 id=\"it-only-requires-one-function-call-to-array_splice\"><strong><span style=\"color: #ff6600;\">It only requires one function call to array_splice:<\/span><\/strong><\/h4>\n[pastacode lang=\u201dphp\u201d manual=\u201d%24original%20%3D%20array(%20%E2%80%98w\u2019%2C%20%E2%80%98i\u2019%2C%20%E2%80%98k\u2019%2C%20%E2%80%98e\u2019%2C%20%E2%80%98t\u2019%20)%3B%0A%24inserted%20%3D%20array(%20\u2019x\u2019%20)%3B%20%2F%2F%20Not%20necessarily%20an%20array%0A%0Aarray_splice(%20%24original%2C%203%2C%200%2C%20%24inserted%20)%3B%20%2F%2F%20splice%20in%20at%20position%203%0A%2F%2F%20%24original%20is%20now%20w%20i%20k%20x%20e%20t%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 2:<\/label><\/p>\n<h4 id=\"here-is-the-solution-for-insert-arrays\"><strong><span style=\"color: #808000;\">Here is the solution for insert arrays:<\/span><\/strong><\/h4>\n[pastacode lang=\u201dphp\u201d manual=\u201dfunction%20array_insert(%26%24array%2C%20%24value%2C%20%24index)%0A%7B%0A%20%20%20%20return%20%24array%20%3D%20array_merge(array_splice(%24array%2C%20max(0%2C%20%24index%20-%201))%2C%20array(%24value)%2C%20%24array)%3B%0A%7D%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 3:<\/label><\/p>\n<h4 id=\"a-function-that-can-insert-at-both-integer-and-string-positions\"><strong><span style=\"color: #800080;\">A function that can insert at both integer and string positions:<\/span><\/strong><\/h4>\n[pastacode lang=\u201dphp\u201d manual=\u201d%2F**%0A%20*%20%40param%20array%20%20%20%20%20%20%24array%0A%20*%20%40param%20int%7Cstring%20%24position%0A%20*%20%40param%20mixed%20%20%20%20%20%20%24insert%0A%20*%2F%0Afunction%20array_insert(%26%24array%2C%20%24position%2C%20%24insert)%0A%7B%0A%20%20%20%20if%20(is_int(%24position))%20%7B%0A%20%20%20%20%20%20%20%20array_splice(%24array%2C%20%24position%2C%200%2C%20%24insert)%3B%0A%20%20%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20%20%20%24pos%20%20%20%3D%20array_search(%24position%2C%20array_keys(%24array))%3B%0A%20%20%20%20%20%20%20%20%24array%20%3D%20array_merge(%0A%20%20%20%20%20%20%20%20%20%20%20%20array_slice(%24array%2C%200%2C%20%24pos)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%24insert%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20array_slice(%24array%2C%20%24pos)%0A%20%20%20%20%20%20%20%20)%3B%0A%20%20%20%20%7D%0A%7D%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h4 id=\"integer-usage\"><strong><span style=\"color: #ff6600;\">Integer usage:<\/span><\/strong><\/h4>\n[pastacode lang=\u201dphp\u201d manual=\u201d%24arr%20%3D%20%5B%22one%22%2C%20%22two%22%2C%20%22three%22%5D%3B%0Aarray_insert(%0A%20%20%20%20%24arr%2C%0A%20%20%20%201%2C%0A%20%20%20%20%22one-half%22%0A)%3B%0A%2F%2F%20-%3E%0Aarray%20(%0A%20%200%20%3D%3E%20\u2019one\u2019%2C%0A%20%201%20%3D%3E%20\u2019one-half\u2019%2C%0A%20%202%20%3D%3E%20\u2019two\u2019%2C%0A%20%203%20%3D%3E%20\u2019three\u2019%2C%0A)%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h4 id=\"string-usage\"><strong><span style=\"color: #808000;\">String Usage:<\/span><\/strong><\/h4>\n[pastacode lang=\u201dphp\u201d manual=\u201d%24arr%20%3D%20%5B%0A%20%20%20%20%22name%22%20%20%3D%3E%20%5B%0A%20%20%20%20%20%20%20%20%22type%22%20%20%20%20%20%20%3D%3E%20%22string%22%2C%0A%20%20%20%20%20%20%20%20%22maxlength%22%20%3D%3E%20%2240%22%2C%0A%20%20%20%20%5D%2C%0A%20%20%20%20%22email%22%20%3D%3E%20%5B%0A%20%20%20%20%20%20%20%20%22type%22%20%20%20%20%20%20%3D%3E%20%22email%22%2C%0A%20%20%20%20%20%20%20%20%22maxlength%22%20%3D%3E%20%22160%22%2C%0A%20%20%20%20%5D%2C%0A%5D%3B%0A%0Aarray_insert(%0A%20%20%20%20%24arr%2C%0A%20%20%20%20%22email%22%2C%0A%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%22phone%22%20%3D%3E%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22type%22%20%20%20%3D%3E%20%22string%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22format%22%20%3D%3E%20%22phone%22%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%5D%0A)%3B%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[pastacode lang=\u201dphp\u201d manual=\u201d%2F%2F%20-%3E%0Aarray%20(%0A%20%20\u2019name\u2019%20%3D%3E%0A%20%20array%20(%0A%20%20%20%20\u2019type\u2019%20%3D%3E%20\u2019string\u2019%2C%0A%20%20%20%20\u2019maxlength\u2019%20%3D%3E%20\u201940\u2019%2C%0A%20%20)%2C%0A%20%20\u2019phone\u2019%20%3D%3E%0A%20%20array%20(%0A%20%20%20%20\u2019type\u2019%20%3D%3E%20\u2019string\u2019%2C%0A%20%20%20%20\u2019format\u2019%20%3D%3E%20\u2019phone\u2019%2C%0A%20%20)%2C%0A%20%20\u2019email\u2019%20%3D%3E%0A%20%20array%20(%0A%20%20%20%20\u2019type\u2019%20%3D%3E%20\u2019email\u2019%2C%0A%20%20%20%20\u2019maxlength\u2019%20%3D%3E%20\u2019160\u2019%2C%0A%20%20)%2C%0A)%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p><label class=\"label label-info\">SOLUTION 4:<\/label><\/p>\n<h4 id=\"we-can-use-this\"><strong><span style=\"color: #800080;\">We can use this:<\/span><\/strong><\/h4>\n[pastacode lang=\u201dphp\u201d manual=\u201dforeach%20(%24array%20as%20%24key%20%3D%3E%20%24value)%20%0A%7B%0A%20%20%20%20if(%24key%3D%3D1)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%24new_array%5B%5D%3D%24other_array%3B%0A%20%20%20%20%7D%20%20%20%0A%20%20%20%20%24new_array%5B%5D%3D%24value%3B%20%20%20%20%0A%7D%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 5:<\/label><\/p>\n<h4 id=\"here-is-the-another-solution\"><span style=\"color: #ff6600;\"><strong>Here is the another Solution:\u00a0\u2013<\/strong><\/span><\/h4>\n<h4 id=\"php-code\"><strong><span style=\"color: #808000;\">PHP Code:<\/span><\/strong><\/h4>\n[pastacode lang=\u201dphp\u201d manual=\u201d%3C%3Fphp%20%20%0A%24original%20%3D%20array(%20\u20191\u2019%2C\u20192\u2019%2C\u20193\u2019%2C\u20194\u2019%2C\u20195\u2019%20)%3B%20%20%0Aecho%20\u2019Original%20array%20%3A%20\u2032.%22%5Cn%22%3B%20%20%0Aforeach%20(%24original%20as%20%24x)%20%20%20%0A%7Becho%20%22%24x%20%22%3B%7D%20%20%0A%24inserted%20%3D%20\u2019%24\u2019%3B%20%20%0Aarray_splice(%20%24original%2C%203%2C%200%2C%20%24inserted%20)%3B%20%20%20%0Aecho%20%22%20%5Cn%20After%20inserting%20\u2019%24\u2019%20the%20array%20is%20%3A%20%22.%22%5Cn%22%3B%20%20%0Aforeach%20(%24original%20as%20%24x)%20%20%20%0A%7Becho%20%22%24x%20%22%3B%7D%20%20%0Aecho%20%22%5Cn%22%20%20%0A%3F%3E%20%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h4 id=\"sample-output\"><strong><span style=\"color: #800080;\">Sample Output:<\/span><\/strong><\/h4>\n<p><strong>Original array :<\/strong><br \/>\n1 2 3 4 5<br \/>\n<strong>After inserting \u2018$\u2019 the array is<\/strong> :<br \/>\n1 2 3 $ 4 5<\/p>\n<p><label class=\"label label-info\">SOLUTION 6:<\/label><\/p>\n<ul>\n<li>splice method can be used for adding and\/or removing elements from an array.<\/li>\n<li>The first argument specifies the location at which to begin adding or removing elements.<\/li>\n<li>The second argument specifies the number of elements to delete.<\/li>\n<li>When using splice to add elements to an array, the second argument would be zero.<\/li>\n<li>The third and subsequent arguments are elements to be added to the array.<\/li>\n<\/ul>\n[pastacode lang=\u201dphp\u201d manual=\u201dvar%20ar%20%3D%20%5B1%2C%202%2C%203%2C%204%2C%205%2C%206%5D%3B%0A%2F%2F%20arguments%3A%20start%20position%2C%20number%20of%20elements%20to%20delete%2C%20elements%20to%20add%0Aar.splice(3%2C%200%2C%20%E2%80%98d\u2019%2C%20%E2%80%98e\u2019%2C%20%E2%80%98f\u2019)%3B%0A%0Aconsole.log(%20ar%20)%3B%20%2F%2F%20%5B1%2C%202%2C%203%2C%20%E2%80%9Cd%22%2C%20%E2%80%9Ce%22%2C%20%E2%80%9Cf%22%2C%204%2C%205%2C%206%5D%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p><label class=\"label label-info\">SOLUTION 7:<\/label><\/p>\n<h4 id=\"hint-for-adding-an-element-at-the-beginning-of-an-array\"><strong><span style=\"color: #ff6600;\">Hint for adding an element at the beginning of an array:<\/span><\/strong><\/h4>\n[pastacode lang=\u201dphp\u201d manual=\u201d%24a%20%3D%20array(\u2018first\u2019%2C%20\u2019second\u2019)%3B%0A%24a%5B-1%5D%20%3D%20\u2019i%20am%20the%20new%20first%20element\u2019%3B%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h4 id=\"then\"><strong><span style=\"color: #808000;\">then:<\/span><\/strong><\/h4>\n[pastacode lang=\u201dphp\u201d manual=\u201dforeach(%24a%20as%20%24aelem)%0A%20%20%20%20echo%20%24a%20.%20\u2019%20\u2019%3B%0A%2F%2Freturns%20first%2C%20second%2C%20i%20am\u2026%0A%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h4 id=\"but\"><strong><span style=\"color: #800080;\">but:<\/span><\/strong><\/h4>\n[pastacode lang=\u201dphp\u201d manual=\u201dfor%20(%24i%20%3D%20-1%3B%20%24i%20%3C%20count(%24a)-1%3B%20%24i%2B%2B)%0A%20%20%20%20%20echo%20%24a%20.%20\u2019%20\u2019%3B%0A%2F%2Freturns%20i%20am%20as%201st%20element%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 8:<\/label><\/p>\n<ul>\n<li>Add Elements to the Beginning of an Array:<\/li>\n<li>unshift method is used to add elements to the beginning of an array.<\/li>\n<li>It accepts multiple arguments, adjusts the indexes of existing elements, and returns the new length of the array.<\/li>\n<li>The unshift method modifies the array on which it is invoked.<\/li>\n<\/ul>\n<h4 id=\"first-we-invoke-unshift-passing-a-single-argument-then-multiple-arguments-displaying-the-results-using-console-log\">First we invoke unshift passing a single argument, then multiple arguments, displaying the results using console.log:<\/h4>\n[pastacode lang=\u201dphp\u201d manual=\u201dvar%20ar%20%3D%20%5B\u2019one\u2019%2C%20\u2019two\u2019%2C%20\u2019three\u2019%5D%3B%0A%2F%2F%20add%20single%20element%0Aar.unshift(\u2018zero\u2019)%3B%0Aconsole.log(%20ar%20)%3B%20%2F%2F%20%5B%22zero%22%2C%20%22one%22%2C%20%22two%22%2C%20%22three%22%5D%0A%0A%2F%2F%20add%20multiple%20elements%0Aar.unshift(0%2C%201%2C%202%2C%203)%3B%0Aconsole.log(%20ar%20)%3B%20%2F%2F%20%5B0%2C%201%2C%202%2C%203%2C%20%22zero%22%2C%20%22one%22%2C%20%22two%22%2C%20%22three%22%5D%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 9:<\/label><\/p>\n<h4 id=\"here-is-simple-function-for-insert-new-element-after-a-specific-key-while-preserving-integer-keys\"><strong>Here is simple function for insert new element after a specific key, while preserving integer keys:<\/strong><\/h4>\n[pastacode lang=\u201dphp\u201d manual=\u201dprivate%20function%20arrayInsertAfterKey(%24array%2C%20%24afterKey%2C%20%24key%2C%20%24value)%7B%0A%20%20%20%20%24pos%20%20%20%3D%20array_search(%24afterKey%2C%20array_keys(%24array))%3B%0A%0A%20%20%20%20return%20array_merge(%0A%20%20%20%20%20%20%20%20array_slice(%24array%2C%200%2C%20%24pos%2C%20%24preserve_keys%20%3D%20true)%2C%0A%20%20%20%20%20%20%20%20array(%24key%3D%3E%24value)%2C%0A%20%20%20%20%20%20%20%20array_slice(%24array%2C%20%24pos%2C%20%24preserve_keys%20%3D%20true)%0A%20%20%20%20)%3B%0A%7D%20%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 10:<\/label><\/p>\n<h4 id=\"normally-with-scalar-values\"><strong><span style=\"color: #ff6600;\">Normally, with scalar values:<\/span><\/strong><\/h4>\n[pastacode lang=\u201dphp\u201d manual=\u201d%24elements%20%3D%20array(\u2018foo\u2019%2C%20\u2026)%3B%0Aarray_splice(%24array%2C%20%24position%2C%20%24length%2C%20%24elements)%3B%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h4 id=\"to-insert-a-single-array-element-into-our-array-dont-forget-to-wrap-the-array-in-an-array-as-it-was-a-scalar-value\">To insert a single array element into our array don\u2019t forget to wrap the array in an array (as it was a scalar value!):<\/h4>\n[pastacode lang=\u201dphp\u201d manual=\u201d%24element%20%3D%20array(\u2018key1\u2019%3D%3E\u2019value1\u2019)%3B%0A%24elements%20%3D%20array(%24element)%3B%0Aarray_splice(%24array%2C%20%24position%2C%20%24length%2C%20%24elements)%3B%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>PROBLEM : How can we insert a new item into an array on any position, say for example in the middle of array? or How can we insert a new item into an array on any position? php arrays insert SOLUTION 1: It only requires one function call to array_splice: [pastacode lang=\u201dphp\u201d manual=\u201d%24original%20%3D%20array(%20%E2%80%98w\u2019%2C%20%E2%80%98i\u2019%2C%20%E2%80%98k\u2019%2C%20%E2%80%98e\u2019%2C%20%E2%80%98t\u2019%20)%3B%0A%24inserted%20%3D%20array(%20\u2019x\u2019%20)%3B%20%2F%2F%20Not%20necessarily%20an%20array%0A%0Aarray_splice(%20%24original%2C%203%2C%200%2C%20%24inserted%20)%3B%20%2F%2F%20splice%20in%20at%20position%203%0A%2F%2F%20%24original%20is%20now%20w%20i%20k%20x%20e%20t%0A\u201d message=\u201dPhp Code\u201d [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[2556,3906,3909,3908,3913,3911,3910,2562,3907,3912],"class_list":["post-1856","post","type-post","status-publish","format-standard","hentry","category-php","tag-how-to-check-if-php-array-is-associative-or-sequential","tag-how-to-insert-an-item-into-an-array-at-a-specific-index","tag-insert-array-content-into-database-php","tag-insert-array-of-new-item-in-any-position-in-another-array","tag-inserting-array-into-php-array","tag-php-insert-an-array-inside-a-multidimensionnal-array","tag-php-insert-item-at-any-array-position-with-key","tag-php-array-delete-by-value-not-key","tag-php-array-insert","tag-php-mysql-insert-array"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1856","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=1856"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1856\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=1856"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=1856"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=1856"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}