Viewed   154 times

I have the following associative array of column data:

$where = array(
    'id'=>array(
        12,
        13,
        14
    ),
    'date'=>array(
        '1999-06-12',
        '2000-03-21',
        '2006-09-31'
    )
);

I need to transpose / rotate the structure to be an array of rows (with merged column data assigned to their respective row). I don't need the column names in the result.

Expected output:

$comb = array(
    array(12, '1999-06-12'),
    array(13, '2000-03-21'),
    array(14, '2006-09-31')
);

 Answers

4

As Kris Roofe stated in his deleted answer, array_column is indeed a more elegant way. Just be sure to put it into some kind of a foreach loop, similar to what Sahil Gulati showed you. For example, like this:

$result = array();

foreach($where['id'] as $k => $v)
{
  $result[] = array_column($where, $k);
}

The var_dump output of $result is exactly what you're looking for

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"
  }
}
Thursday, August 25, 2022
2

Just walk the array in the correct order. Assuming you have relatively small arrays, the easiest solution is just to create a brand new array during that walk.

A solution will be of the form:

$rows = count($arr);
$ridx = 0;
$cidx = 0;

$out = array();

foreach($arr as $rowidx => $row){
    foreach($row as $colidx => $val){
        $out[$ridx][$cidx] = $val;
        $ridx++;
        if($ridx >= $rows){
            $cidx++;
            $ridx = 0;
        }
    }
}
Thursday, September 15, 2022
 
3

You can do it based on array_column():-

<?php

$array = [[5, 4, 10], [11, 13, 15], [32, 14, 15]];

$final_array = [array_column($array,0),array_column($array,1),array_column($array,2)];

print_r($final_array );

Output:-https://eval.in/836310

Note:- above code will work only for this array.

More general and considering all aspects code is using foreach():-

<?php
$array = [[5, 4, 10], [11, 13, 15], [32, 14, 15]];

$final_array = array();

foreach($array as $arr){
    foreach($arr as $key=>$value){
      $final_array[$key][]=$value;
    }
}
print_r($final_array);

Output:- https://eval.in/836313

Sunday, December 4, 2022
 
5

You could use toArray(T[]).

import java.util.*;
public class Test{
    public static void main(String[] a){ 
        List<String[]> list=new ArrayList<String[]>();
        String[][] matrix=new String[list.size()][];
        matrix=list.toArray(matrix);
    }   
}

Javadoc

Friday, November 11, 2022
 
5

The prefix idea is good but this part doesn't work because you might get multiple results:

$result .= $key .'/'. flatten_directory($value, $prefix . $key . '.');

Rather than returning a single string, return an array of strings. You're also mixing up when to use $key and $value.

function flatten_directory($directory, $prefix = "") {
    $result = array();
    foreach ($directory as $key => $part) {
        if (is_array($part)) {
            $result = array_merge($result, flatten_directory($part, $prefix . "/" . $key));
        } else {
            $result[] = $prefix . "/" . $part . "/";
        }
    }
    return $result;
}

Output:

Array
(
    [0] => /CANON/DCIM/100CANON/
    [1] => /CANON/DCIM/101CANON/
    [2] => /CANON/DCIM/CANONMSC/
    [3] => /CANON/CANON_SC/IMAGE/0001/
    [4] => /CANON/CANON_SC/DOCUMENT/0001/
    [5] => /CANON/MISC/
)
Monday, December 12, 2022
 
brand
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share