Restructure Multidimensional Column Data to Row Data
The goal is to transform a given multidimensional array of column data into its corresponding row data structure. In this scenario, we start with the following column data:
$where = array( 'id' => array( 12, 13, 14 ), 'date' => array( '1999-06-12', '2000-03-21', '2006-09-31' ) );
We wish to reshape this structure into rows with combined column data.
Solution using Loop and array_column
One approach involves a loop and the use of the array_column() function:
$result = array(); foreach ($where['id'] as $k => $v) { $result[] = array_column($where, $k); }
In this solution, we iterate through the id column and use array_column() to extract the corresponding values from the date column. The result will be an array of arrays, each representing a row in the desired format:
array(3) { [0] => array(2) { [0] => int(12) [1] => string(10) "1999-06-12" } [1] => array(2) { [0] => int(13) [1] => string(10) "2000-03-21" } [2] => array(2) { [0] => int(14) [1] => string(10) "2006-09-31" } }
The above is the detailed content of How to Transform Multidimensional Column Data into Row Data in PHP?. For more information, please follow other related articles on the PHP Chinese website!