Asked  8 Months ago    Answers:  5   Viewed   40 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

70

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"
  }
}
Wednesday, March 31, 2021
 
AntoineB
answered 8 Months ago
72

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;
        }
    }
}
Wednesday, March 31, 2021
 
RompelStompel
answered 8 Months ago
98

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

Saturday, May 29, 2021
 
Lloydworth
answered 5 Months ago
83

So basically you need to display "No." in top left corner?

That area is called TopLeftHeaderCell. It is just like the other DataGridViewCell, you can set its Value property to achieve the task.

dataGridView.TopLeftHeaderCell.Value = "No.";
Friday, August 6, 2021
 
mpen
answered 3 Months ago
64

It seems you need read_csv for DataFrame first with filter only second and third column first and then convert to numpy array by values: import pandas as pd from sklearn.cluster import KMeans from pandas.compat import StringIO

temp=u"""col,iid,rat
4,1,0
5,2,4
6,3,3
7,4,1"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
df = pd.read_csv(StringIO(temp), usecols = [1,2])
print (df)
   iid  rat
0    1    0
1    2    4
2    3    3
3    4    1

X = df.values 
print (X)
[[1 0]
 [2 4]
 [3 3]
 [4 1]]

kmeans = KMeans(n_clusters=2)
a = kmeans.fit(X)
print (a)
KMeans(algorithm='auto', copy_x=True, init='k-means++', max_iter=300,
    n_clusters=2, n_init=10, n_jobs=1, precompute_distances='auto',
    random_state=None, tol=0.0001, verbose=0)
Friday, August 20, 2021
 
timothy5216
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share