![]() ![]() ![]() If you don't want to lose duplicates, and you're ok, with having the values in the flipped array in an array as well, you may use this: $ArrayOut ]]= $Value Įlse $ArrayOut ]= $Value Require a string value in the data part of the array that is keyed to $DesiredKey2 If $DesiredKey2 was specified then assume a multidimensional array is desired and build it If (! is_string ( $Value )) return false Require a string value in the data part of the array that is keyed to $DesiredKey If there is an original key that need to be preserved as data in the new array then do that if requested ($OrigKeyName=true) I needed a way to flip a multidimensional array and came up with this function to accomplish the task. In the example from the very beginning array will become: Well, and to achieve that "last comments" effect, just do: $array = array_flip(array_unique($array)) To achieve what I need I came up with the following (in case someone will need to do something like that):įirst, we need a way to flip an array, taking the first encountered key for each of values in array. In fact it wasn't (see above, as it is the order of values used). I had a sorted comment-id => entry-id array, and what popped in my mind is just do array_flip($array), and I thought I now would have last five entries in the array as most recently commented entry => comment pairs. ![]() ![]() In my application I needed to find five most recently commented entries. When you do array_flip, it takes the last key accurence for each value, but be aware that keys order in flipped array will be in the order, values were first seen in original array. Getting Started Introduction A simple tutorial Language Reference Basic syntax Types Variables Constants Expressions Operators Control Structures Functions Classes and Objects Namespaces Enumerations Errors Exceptions Fibers Generators Attributes References Explained Predefined Variables Predefined Exceptions Predefined Interfaces and Classes Predefined Attributes Context options and parameters Supported Protocols and Wrappers Security Introduction General considerations Installed as CGI binary Installed as an Apache module Session Security Filesystem Security Database Security Error Reporting User Submitted Data Hiding PHP Keeping Current Features HTTP authentication with PHP Cookies Sessions Dealing with XForms Handling file uploads Using remote files Connection handling Persistent Database Connections Command line usage Garbage Collection DTrace Dynamic Tracing Function Reference Affecting PHP's Behaviour Audio Formats Manipulation Authentication Services Command Line Specific Extensions Compression and Archive Extensions Cryptography Extensions Database Extensions Date and Time Related Extensions File System Related Extensions Human Language and Character Encoding Support Image Processing and Generation Mail Related Extensions Mathematical Extensions Non-Text MIME Output Process Control Extensions Other Basic Extensions Other Services Search Engine Extensions Server Specific Extensions Session Extensions Text Processing Variable and Type Related Extensions Web Services Windows Only Extensions XML Manipulation GUI Extensions Keyboard Shortcuts ? This help j Next menu item k Previous menu item g p Previous man page g n Next man page G Scroll to bottom g g Scroll to top g h Goto homepage g s Goto search ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |