How can I Transform Multidimensional Column Data into Row Data Using array_column in PHP?

DDD
Release: 2024-10-26 05:35:30
Original
112 people have browsed it

How can I Transform Multidimensional Column Data into Row Data Using array_column in PHP?

Restructuring Multidimensional Column Data into Row Data Using array_column

Some programming tasks, such as exchanging columns for rows in a multidimensional array, can be simplified with the right tools. In this case, the array_column function offers a straightforward solution.

To transform an associative array of column data into a multidimensional array of row data, follow these steps:

  1. Create an empty array to store the results.
  2. Iterate through the keys of the original array.
  3. For each key, use the array_column function to extract the corresponding column values.
  4. Add the extracted values as a new row to the results array.

Here's the code to achieve this transformation:

<code class="php">$result = array();

foreach($where['id'] as $k => $v) {
  $result[] = array_column($where, $k);
}</code>
Copy after login

The resulting array will now be structured as:

<code class="php">array(
    array(12, '1999-06-12'),
    array(13, '2000-03-21'),
    array(14, '2006-09-31')
);</code>
Copy after login

which matches the desired output.

This method leverages the power of array_column, a built-in PHP function designed for such array manipulations. By combining iteration and column extraction, we can seamlessly transform the data into the desired format with ease.

The above is the detailed content of How can I Transform Multidimensional Column Data into Row Data Using array_column 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
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!