【问题标题】:Separating multiple arrays of items into two columns in a balanced way以平衡的方式将多个项目数组分成两列
【发布时间】:2017-11-22 11:34:00
【问题描述】:

抱歉,标题不具描述性,我只是不知道如何描述。在我的网站上,我有一个常见问题解答页面,该页面上有主题组,每个主题都有问题。例如:

FAQ
  - Topic 1
     - Question
     - Question
     - Question
  - Topic 2
     - Question
     - Question
     - Question
  - Topic 3
     - Question
     - Question
     - Question
     - Question

我需要将这些主题分成两部分,以使它们保持平衡。例如,将上述主题分成两列的最平衡方式是在左列中包含主题 1 和主题 2(60% 的问题),然后在右列中包含主题 3(40% 的问题)。它看起来像这样:

我无法弄清楚我需要做什么背后的逻辑。我现在这样做的方式是计算每个块的百分比,然后遍历块并将它们放入第一列,直到增量百分比大于 50,然后切换到第二列。这最终看起来像这样:

显然这是不平衡的。这个看似简单的任务实际上非常复杂,还是我在这里完全遗漏了什么?我正在用 PHP 编写代码,但我也标记了 JavaScript,因为这似乎更像是一个逻辑问题。

【问题讨论】:

  • 对于javascript有一个masonry插件,你可以看看。
  • 你问我们什么是你的逻辑?总问题 = 10。中点 = 5。当左侧计数小于中点时,将 groups 添加到左侧,然后将剩余的组转储到右侧。正确的?有没有这样的情况不能提供预期的效果? ...并且 PHP 可以解决逻辑问题。你会向我们提供一系列问题吗?或任何要修复的代码?我们如何回答?
  • @mickmackusa 中点技术是我所说的我已经在做的,但没有以平衡的方式划分它们,因为如果你有 30/30/60,它将把 30 放在第一列和 30/60 在第二列。第二张图是 50% 中点的结果。

标签: javascript php arrays logic


【解决方案1】:

如果我正确理解逻辑,您希望填充左侧直到达到或超过 50%,然后将其余部分倒入右侧。

这里我使用count()recursiveregular形式来计算问题的数量,然后我找到中点。

PHP: (Demo)

$FAQ=[
    'Topic 1'=>['Question1','Question2','Question3'],
    'Topic 2'=>['Question4','Question5','Question6'],
    'Topic 3'=>['Question7','Question8','Question9','Question10']
];
$mid_point=(count($FAQ,1)-count($FAQ))/2;

$tally=0;
foreach($FAQ as $k=>$a){
    if($tally<=$mid_point){
        $leftside[$k]=$a;
    }else{
        $rightside[$k]=$a;
    }
    $tally+=count($a);
}

var_export($leftside);
echo "\n\n";
var_export($rightside);

输出:

// leftside array:
array (
  'Topic 1' => 
  array (
    0 => 'Question1',
    1 => 'Question2',
    2 => 'Question3',
  ),
  'Topic 2' => 
  array (
    0 => 'Question4',
    1 => 'Question5',
    2 => 'Question6',
  ),
)

// rightside array
array (
  'Topic 3' => 
  array (
    0 => 'Question7',
    1 => 'Question8',
    2 => 'Question9',
    3 => 'Question10',
  ),
)

Here is another demo 其中主题 1 有 5 个问题,主题 2 有 3 个问题,主题 3 有 1 个问题。

【讨论】:

  • @Gavin 你没有提供任何代码,所以我只是模拟了一些来表达逻辑。这就是你要拍摄的吗?如果这些问题来自数据库,您可以只计算 num 行并除以 2。
  • 谢谢。它仍然没有平衡我想要的项目。例如,使用Topic 1 中的 5 个项目、Topic 2 中的 3 个项目和 Topic 3 中的 1 个项目尝试此代码。它最终会将Topic 1Topic 2 放在第一列(8 个问题),将Topic 3 单独放在第三列,只有一个问题。
  • @Gavin 删除 round() 修复了这种情况:sandbox.onlinephpfunctions.com/code/…
  • @Gavin 是否有我的解决方案让您失望的情况?我相信我的解决方案比您发布的解决方案更简单。如果它没有为您中断,请授予它绿色勾号,以便您的答案在系统中被视为已解决。如果它中断,请提供中断它的输入数组。
【解决方案2】:

很抱歉回答我自己的问题。这是我想出来的。我确定这不是最好的方法,所以如果您有更好的方法,请告诉我。

$sections = array (
    array(
        'section_title' => 'Section 1',
        'items' => array (
            'Item 1',
            'Item 2',
            'Item 3'
        )
    ),
    array(
        'section_title' => 'Section 2',
        'items' => array (
            'Item 1',
            'Item 2',
            'Item 3'
        )
    ),
    array(
        'section_title' => 'Section 3',
        'items' => array (
            'Item 1',
            'Item 2',
            'Item 3',
            'Item 4'
        )
    )
);

$left_col = array();
$right_col = $sections; // Put all items into the right column initially

$left_col_height = 0;
$right_col_height = 0;

foreach ( $right_col as $arr ) {
    $right_col_height += count( $arr[ 'items' ] ); // Get the count of all items in the right column
}

foreach ( $right_col as $key => $val ) {
    // Get height/count of the current block of items
    $block_height = count( $val[ 'items' ] );
    // Get difference between height of right column and left column
    $cur_col_diff = $right_col_height - $left_col_height;
    // Calculate what the heights of the columns would be if the block was moved
    $next_left_col_height = $left_col_height + $block_height;
    $next_right_col_height = $right_col_height - $block_height;
    // Calculate what the difference between the two columns would be if the block was moved
    $next_col_diff = abs( $next_right_col_height - $next_left_col_height );
    if ( $next_col_diff < $cur_col_diff ) {
        // The difference betweens the columns would be less if the block was moved, so move it
        array_push( $left_col, $val );
        unset( $right_col[ $key ] );
        $left_col_height += $block_height;
        $right_col_height -= $block_height;
    }
    else
        break;
}

Here's the working PHP code.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    • 2015-07-10
    • 2021-01-06
    • 1970-01-01
    • 2023-01-25
    • 2014-12-22
    相关资源
    最近更新 更多