【问题标题】:Move 2nd level sub-array to the top of the 1st level multidimentional array based on value of the sub-array根据子数组的值将第二级子数组移动到第一级多维数组的顶部
【发布时间】:2021-06-09 18:14:23
【问题描述】:

我正在循环一个多维数组,并留下一些值。

这是完整的 PHP 代码。


<?php 
/**
 * Each designer is required 3x per week, 
 * add their name 3 times to allow for the count.
 * 
 * [array_pop]  removes the designer from the sub-array
 * u[sort($designers, "sortByTotalItems")] then moves the designers with more names remaining to the top of the list
 *
 * @var [type]
 */
$designers = array(
    [ "Aaron Summers", "Aaron Summers", "Aaron Summers" ],
    [ "Adam Smart", "Adam Smart", "Adam Smart" ],
    [ "Andrew Montgomery", "Andrew Montgomery", "Andrew Montgomery" ],
    [ "Dave Robertson", "Dave Robertson", "Dave Robertson" ],
    [ "Holly Colby", "Holly Colby", "Holly Colby" ],
    [ "Karen Fok", "Karen Fok", "Karen Fok" ],
    [ "Kieran Lintott", "Kieran Lintott", "Kieran Lintott" ],
    [ "Rebecca Roffey", "Rebecca Roffey", "Rebecca Roffey" ],
    [ "Sonia Tam", "Sonia Tam", "Sonia Tam" ],
    [ "Steffi Wallace", "Steffi Wallace", "Steffi Wallace" ],
    [ "Taz Patel", "Taz Patel", "Taz Patel" ]
);
/**
 * Store the original designers to reset them once empty
 *
 * @var [type]
 */
$designers_bak = $designers;
/**
 * Generate an array of string dates between 2 dates
 *
 * @param string $start Start date
 * @param string $end End date
 * @param string $format Output format (Default: Y-m-d)
 *
 * @return array
 */
function getDatesFromRange( $start, $end ) {
    $array = array();
    $interval = new DateInterval('P1D');

    $realEnd = new DateTime($end);
    $realEnd->add($interval);

    $period = new DatePeriod(new DateTime($start), $interval, $realEnd);
    
    foreach($period as $date) { 
        $dayOfWeek = $date->format('l');
        if ( $dayOfWeek != 'Saturday' && $dayOfWeek != 'Sunday' ) {
            $array[] = $date->format('Y-m-d'); 
        }
    }

    return $array;
}
/**
 * Sort the array into sub-arrays that contain the most values
 * array(
 *     array('name', 'name', 'name')
 *     array('name', 'name')
 *     array('name')
 * )
 * https://stackoverflow.com/questions/7433569/php-sort-a-multidimensional-array-by-number-of-items#answer-7433753
 */
function sortByTotalItems($a, $b) {
    if ($a == $b) {
        return 0;
    }
    return (count($a) > count($b)) ? -1 : 1;
}
/**
 * Get the total amount of days in this week 1-5
 *
 * @var [type]
 */
$days_at_work = 3;
$total_days_in_week = 5;
$assigned_total = floor(count( $designers ) * $days_at_work / $total_days_in_week);    

/**
 * Each day
 *
 * @var [type]
 */
$working_weeks = array_chunk(getDatesFromRange('09-06-2021', '09-06-2022'), $total_days_in_week);
$given_designer = array();
foreach ( $working_weeks as $working_days ) {
    /**
     * Reset the designers
     *
     * @var [type]
     */
    foreach ( $working_days as $working_day ) {

        /**
         * Randomise the designers
         *
         * @var [type]
         */
        shuffle( $designers ); 
        /**
         * Sort the shuffle, so the sub-arrays count is descending
         * array(
         *     array('name', 'name', 'name')
         *     array('name', 'name')
         *     array('name')
         * )
         *
         * @var [$designers]
         */
        $designers = array_filter(array_map('array_filter', $designers));
        usort($designers, "sortByTotalItems");



        /**
         * Add designer to the working day
         *
         * @var [type]
         */

        $forcount = ( count($designers) < $assigned_total ) ? count($designers) : $assigned_total;



        for ($d=0; $d < $forcount; $d++) {
            /**
             * Add designer to the days visit
             *
             * @var [type]
             */

            $given_designer[] = array( 
                                    "title"=> $designers[$d][0],
                                    "start"=> $working_day
                                );
            /**
             * Remove the name from the names array -1 each loop
             *
             * @var [type]
             */
            array_pop($designers[$d]);
            /**
             * remove empty arrays so we can count the remaining designers
             * If there are less than 1 weeks worth of designers, reset the variable
             */
            $current_designers = array_filter(array_map('array_filter', $designers));
            if (  count($current_designers) < $assigned_total ) {
                $designers = $designers_bak;
            }
        }

        
    }  
}
?>

下面是我在循环和 array_pop 之后剩下的内容。

我们称之为 array1

<?php 
array ( 0 => array ( ),
        1 => array ( ),
        2 => array ( ),
        3 => array ( ),
        4 => array ( 0 => 'Aaron Summers', ),
        5 => array ( 0 => 'Taz Patel', ),
        6 => array ( 0 => 'Sonia Tam', ),
        7 => array ( 0 => 'Adam Smart', ),
        8 => array ( 0 => 'Andrew Montgomery', ),
    ) 
?>

然后我用原始值重置数组:

我们称之为 array2

<?php
array ( 0 => array ( 0 => 'Aaron Summers', 1 => 'Aaron Summers', 2 => 'Aaron Summers', ),
        1 => array ( 0 => 'Adam Smart', 1 => 'Adam Smart', 2 => 'Adam Smart', ),
        2 => array ( 0 => 'Andrew Montgomery', 1 => 'Andrew Montgomery', 2 => 'Andrew Montgomery', ),
        3 => array ( 0 => 'Dave Robertson', 1 => 'Dave Robertson', 2 => 'Dave Robertson', ),
        4 => array ( 0 => 'Holly Colby', 1 => 'Holly Colby', ),
        5 => array ( 0 => 'Karen Fok', 1 => 'Karen Fok', 2 => 'Karen Fok', ),
        6 => array ( 0 => 'Kieran Lintott', 1 => 'Kieran Lintott', 2 => 'Kieran Lintott', ),
        7 => array ( 0 => 'Rebecca Roffey', 1 => 'Rebecca Roffey', 2 => 'Rebecca Roffey', ),
        8 => array ( 0 => 'Sonia Tam', 1 => 'Sonia Tam', 2 => 'Sonia Tam', ),
        9 => array ( 0 => 'Steffi Wallace', 1 => 'Steffi Wallace', 2 => 'Steffi Wallace', ),
        10 => array ( 0 => 'Taz Patel', 1 => 'Taz Patel', 2 => 'Taz Patel', ),
    )
?>

我如何能够根据留在 array1 中的人重新排序 array2 中的项目?

array2 是数组和原始形式的数组的总大小,在我循环并使用 array_pop() 删除名称之前。

【问题讨论】:

  • 首先,您已经在第二个数组中添加了一些数据,请注意解释那里的逻辑
  • 这是数组的总大小吗?或者这在现实中可能是一个非常大的数组
  • array2 是数组的总大小,我已经用更多细节更新了我的问题
  • 好吧,您将不得不分几步完成此操作。首先找到第一个数组中的所有名称并放入一个新数组中,对其进行排序,然后将每个名称的 3 次出现伪造到新数组中2
  • 我想我们应该假设在 array1 中确实有 11 + 空出现,或者我们将如何设法创建丢失的 6 个名称

标签: php arrays


【解决方案1】:

有几种方法可以做到这一点,这相当简单,因为无论如何名称都应该是唯一的:

$array2 = array_merge(array_column(array_filter($array1), null, 0), 
                      array_column($array2, null, 0));
  • 从有序数组$array1 中删除空值,或者对*后的最终结果执行此操作见下文
  • 索引每个数组的值,该值至少应在索引 0 处
  • 将现有数组合并到有序数组中,该数组将按正确顺序替换并将剩余名称添加到末尾

如果您确实需要获取整数索引,请使用array_values

$array2 = array_values(array_filter(array_merge(array_column($array1, null, 0),
                                                array_column($array2, null, 0)));

如果您首先使用名称作为索引,那么无论您对所有这些代码做什么,都可能会更短更简单。

【讨论】:

  • 谢谢 AbraCadaver,我正在将内容输出到 json 文件,因此无法将名称作为键输出。我还在学习如何对数组进行排序,这对我来说是一件很难理解的事情。
【解决方案2】:

这将重新排序$array2(到一个新的变量 $final)。首先,它组装一个简单的名称数组$people,然后通过基于$people 数组的优先级重新组装$array2。这是你想要完成的吗?

<?php 
$array1 = array( 0 => array ( ),
        1 => array ( ),
        2 => array ( ),
        3 => array ( ),
        4 => array ( 0 => 'Aaron Summers', ),
        5 => array ( 0 => 'Taz Patel', ),
        6 => array ( 0 => 'Sonia Tam', ),
        7 => array ( 0 => 'Adam Smart', ),
        8 => array ( 0 => 'Andrew Montgomery', ),
    ) ;
    
    $people = [];
    foreach ($array1 as $a) { 
        if(count($a)>0 && $person = $a[0]) {
            $people[]= $person;
        }
    }

$array2 =array ( 0 => array ( 0 => 'Aaron Summers', 1 => 'Aaron Summers', 2 => 'Aaron Summers', ),
        1 => array ( 0 => 'Adam Smart', 1 => 'Adam Smart', 2 => 'Adam Smart', ),
        2 => array ( 0 => 'Andrew Montgomery', 1 => 'Andrew Montgomery', 2 => 'Andrew Montgomery', ),
        3 => array ( 0 => 'Dave Robertson', 1 => 'Dave Robertson', 2 => 'Dave Robertson', ),
        4 => array ( 0 => 'Holly Colby', 1 => 'Holly Colby', ),
        5 => array ( 0 => 'Karen Fok', 1 => 'Karen Fok', 2 => 'Karen Fok', ),
        6 => array ( 0 => 'Kieran Lintott', 1 => 'Kieran Lintott', 2 => 'Kieran Lintott', ),
        7 => array ( 0 => 'Rebecca Roffey', 1 => 'Rebecca Roffey', 2 => 'Rebecca Roffey', ),
        8 => array ( 0 => 'Sonia Tam', 1 => 'Sonia Tam', 2 => 'Sonia Tam', ),
        9 => array ( 0 => 'Steffi Wallace', 1 => 'Steffi Wallace', 2 => 'Steffi Wallace', ),
        10 => array ( 0 => 'Taz Patel', 1 => 'Taz Patel', 2 => 'Taz Patel', ),
    );
   $final=[];
    foreach($array2 as $a) {
       if (in_array($a[0], $people) !==false) array_unshift($final, $a);
       else $final[]=$a;
    }
    

print_r($final);
?>

输出:

Array
(
    [0] => Array
        (
            [0] => Taz Patel
            [1] => Taz Patel
            [2] => Taz Patel
        )

    [1] => Array
        (
            [0] => Sonia Tam
            [1] => Sonia Tam
            [2] => Sonia Tam
        )

    [2] => Array
        (
            [0] => Andrew Montgomery
            [1] => Andrew Montgomery
            [2] => Andrew Montgomery
        )

    [3] => Array
        (
            [0] => Adam Smart
            [1] => Adam Smart
            [2] => Adam Smart
        )

    [4] => Array
        (
            [0] => Aaron Summers
            [1] => Aaron Summers
            [2] => Aaron Summers
        )

    [5] => Array
        (
            [0] => Dave Robertson
            [1] => Dave Robertson
            [2] => Dave Robertson
        )

    [6] => Array
        (
            [0] => Holly Colby
            [1] => Holly Colby
        )

    [7] => Array
        (
            [0] => Karen Fok
            [1] => Karen Fok
            [2] => Karen Fok
        )

    [8] => Array
        (
            [0] => Kieran Lintott
            [1] => Kieran Lintott
            [2] => Kieran Lintott
        )

    [9] => Array
        (
            [0] => Rebecca Roffey
            [1] => Rebecca Roffey
            [2] => Rebecca Roffey
        )

    [10] => Array
        (
            [0] => Steffi Wallace
            [1] => Steffi Wallace
            [2] => Steffi Wallace
        )

)

https://www.tehplayground.com/zkrdHhL52a1ihqMY

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-05
    • 1970-01-01
    • 2017-04-01
    • 2015-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多