![]() |
Pre- or appending arrays and preserving key => value pairs - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22) +--- Thread: Pre- or appending arrays and preserving key => value pairs (/showthread.php?tid=57825) |
Pre- or appending arrays and preserving key => value pairs - El Forum - 04-15-2013 [eluser]GrahamDj28[/eluser] Hi, Maybe somebody has already posted something like this, but i could not really find a solution for this. So I made my own function and would like to share it with all of you! We all know the array_merge and array_merge_recursive for merging arrays. They both work fine. What i was looking for was a function for prepending or appending values onto an existing array while preserving key => value pairs. When using string keys array_unshift is no problem. But I often have id => title pairs for using in a form select. And then you can't use array_unshift for prepending extra data onto it. My solution Code: /** As you can see there is no check if a key already exists, as I only use this function to prepend an empty select option (''=>Please make a choise) for a required form select Hope this can help somebody |