簡體   English   中英

按字母順序將多維數組排序為2列

[英]sort multidimensional array alphabetically into 2 columns

我有一個看起來像這樣的數組

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 )
      )

我想將其垂直分為2列

所以看起來像這樣

Alfa Romeo      BMW
Aston Martin    Caterham
Audi

現在,當Yii使用checkBoxList()函數生成復選框時,它看起來像這樣

   Alfa Romeo   Aston Martin
   Audi         BMW
   Caterham

現在我有這個排序。 但這僅適用於一維數組

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 ;
}

現有功能並沒有阻止它在n維數組上使用的功能。 實際上,您在此處顯示的功能使用提供的輸入即可准確提供所需的輸出

來自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>';

在這種情況下,您應該將數據添加到新的一維數組中。請嘗試以下操作

 $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);

然后,您可以使用傳遞新數組作為參數的工作函數。

嘗試此操作,添加som條件以顯示在您的兩列中

<?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);
  ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM