Flip two-dimensional associative array in PHP
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Flipping a two-dimensional associative array in PHP means transposing it — converting rows to columns and columns to rows. If the original array maps outer keys to inner key-value pairs, the flipped version maps inner keys to outer key-value pairs. PHP does not have a built-in array_transpose() function, so you build it by iterating through the nested array and reorganizing the keys. The array_flip() function only works on one-dimensional arrays and is not suitable for this operation.
What Flipping Means
Given a 2D array structured as a table:
Flipping (transposing) converts person → subject to subject → person:
Method 1: Nested foreach Loop
This is the most straightforward and readable approach.
Method 2: Reusable Function
Method 3: array_column (Numeric Outer Keys)
For arrays with numeric outer keys and consistent inner keys, array_column() extracts a single column:
Method 4: array_map with Spread Operator
For numerically-indexed 2D arrays:
This uses the special behavior of array_map(null, ...) which zips arrays together. It only works with numeric keys.
Flipping 1D Arrays with array_flip
For simple one-dimensional arrays, array_flip() swaps keys and values:
array_flip() does not work on 2D arrays — it only handles scalar values.
Handling Uneven Inner Arrays
When inner arrays have different keys:
Common Pitfalls
- Using
array_flip()on a 2D array:array_flip()only works with scalar values (strings and integers). Passing a nested array triggers a warning and produces incorrect results. Use a custom transpose function instead. - Assuming all inner arrays have the same keys: If inner arrays have different keys, some entries in the transposed result will be missing. Check for key consistency or provide a default value for missing entries.
- Using
array_map(null, ...$array)with associative keys: Thearray_map(null, ...)trick only works with numerically-indexed arrays. Associative keys are discarded during the spread operation. - Duplicate values in
array_flip()for 1D arrays: When flipping a 1D array, duplicate values cause data loss because the last occurrence overwrites earlier ones. Usearray_count_values()or group manually if duplicates exist. - Mutating the original array: None of these methods modify the original array — they all return new arrays. If you assign the result back to the same variable, the original data is lost.
Summary
- Transpose a 2D associative array by iterating with nested
foreachand swapping outer/inner keys - Use
array_column()to extract specific columns from arrays of associative arrays - Use
array_map(null, ...$array)to transpose numerically-indexed 2D arrays array_flip()only works on 1D arrays with scalar values — it swaps keys and values- Handle uneven inner arrays by collecting all keys first and filling missing values with a default
Related reading
- foreach vs someList.ForEach
- Format y axis as percent
- Four color theorem Java implementation of U.S. map
- Freezing graph to pb in Tensorflow2
- Frequency counts for unique values in a NumPy array
- from keras.backend.tensorflow_backend import set_session
- From list of integers, get number closest to a given value
- From ND to 1D arrays

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.