简体   繁体   中英

sort multidimensional array alphabetically into 2 columns

I have an array that looks like this

Array ( [0] => Array ( [make] => Alfa Romeo [id] => 2 ) 
        [1] => Array ( [make] => Aston Martin [id] => 3 ) 
        [2] => Array ( [make] => Audi [id] => 4 )
        [3] => Array ( [make] => BMW [id] => 8 ) 
        [4] => Array ( [make] => Caterham [id] => 9 )
      )

i could like to sort it vertically into 2 columns

so it would look like this

Alfa Romeo      BMW
Aston Martin    Caterham
Audi

right now it looks like this when Yii generates a checkbox using the checkBoxList() function

   Alfa Romeo   Aston Martin
   Audi         BMW
   Caterham

right now i have this to sort. but this only works with 1 dimensional arrays

function array_chunk_vertical($data, $columns = 2) {
    $n = count($data) ;
    $per_column = floor($n / $columns) ;
    $rest = $n % $columns ;

    // The map
    $per_columns = array( ) ;
    for ( $i = 0 ; $i < $columns ; $i++ ) {
        $per_columns[$i] = $per_column + ($i < $rest ? 1 : 0) ;
    }

    $tabular = array( ) ;
    foreach ( $per_columns as $rows ) {
        for ( $i = 0 ; $i < $rows ; $i++ ) {
            $tabular[$i][ ] = array_shift($data) ;
        }
    }

    return $tabular ;
}

There's nothing about your existing function that prevents it from being used on an n-dimensional array. In fact, the function you show here, gives exactly the desired output using the provided input.

来自eval.in的输出

<?php

function array_chunk_vertical($data, $columns = 2) {
    $n = count($data) ;
    $per_column = floor($n / $columns) ;
    $rest = $n % $columns ;

    // The map
    $per_columns = array( ) ;
    for ( $i = 0 ; $i < $columns ; $i++ ) {
        $per_columns[$i] = $per_column + ($i < $rest ? 1 : 0) ;
    }

    $tabular = array( ) ;
    foreach ( $per_columns as $rows ) {
        for ( $i = 0 ; $i < $rows ; $i++ ) {
            $tabular[$i][ ] = array_shift($data) ;
        }
    }

    return $tabular ;
}


$cars = [
    [ 'make' => 'Alfa Romeo', 'id' => 2 ],
    [ 'make' => 'Aston Martin', 'id' => 3 ],
    [ 'make' => 'Audi', 'id' => 4 ],
    [ 'make' => 'BMW', 'id' => 8 ],
    [ 'make' => 'Caterham', 'id' => 9 ],
];

echo '<table>';
foreach(array_chunk_vertical($cars) as $row) {
    echo '<tr>';
    foreach($row as $car) {
      echo '<td><input type="checkbox" value="', $car['id'], '" />', $car['make'], '</td>';
    }
    echo '</tr>';

}
echo '</table>';

In that case you should add your data into a new one dimension array.Try something like the following

 $a = array(0 => array( 'make' => 'Alfa Romeo', 'id' => 2 ), 
       1 => array( 'make' => 'Aston Martin', 'id' => 3 ), 
       2 => array( 'make' => 'Audi', 'id' => 4 ),
       3 => array( 'make' => 'BMW', 'id' => 8 ), 
       4 => array( 'make' => 'Caterham', 'id' => 9 )
  );
  $b=array();
  foreach ($a as $v)
  {
  $b[]=$v['make'];    
  }
  sort($b);
  print_r($b);

Then you can use your working function passing the new array as the parameter.

try this,add up som condtions to display in your two column

<?php $sort=Array ( 
    0 => Array ( 'make' => 'Alfa Romeo' ,'id' => 2 ), 
    1 => Array ( 'make' => 'Aston Martin' ,'id' => 3 ) ,
    2 => Array ( 'make' => 'Audi' ,'id' => 4 ),
    3 => Array ( 'make' => 'Caterham' ,'id' => 8 ), 
    4 => Array ( 'make' => 'BMW' , 'id' => 9 )
  );
  print_r($sort);
  sort($sort); echo ' <br/>';
  print_r($sort);
  ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM