How to Transform Multidimensional Column Data into Row Data in PHP?

Mary-Kate Olsen
Release: 2024-10-26 05:30:31
Original
737 people have browsed it

How to Transform Multidimensional Column Data into Row Data in PHP?

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'
    )
);
Copy after login

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);
}
Copy after login

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"
  }
}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!