根据值重新排列数组数据并更新键名称

素食主义者

这是一个数组:

$return = Array (
    [9:00] => Array (
        [0] => Array (
            [iScheduleID] => 13                    
            [iAdminID] => 4
            [vStaff] => Ms. Lily
            [vStudent] => Taylor Osborn
            [iPositionID] => 0
        ),
        [1] => Array (
            [iScheduleID] => 14
            [iAdminID] => 4
            [vStaff] => Ms. Lily
            [vStudent] => Smaran Meesala
            [iPositionID] => 0
        ),
        [2] => Array (
            [iScheduleID] => 1
            [iAdminID] => 3
            [vStaff] => Ms. Sanchita
            [vStudent] => Darian Newell
            [iPositionID] => 2
        ),     
        [4] => Array (
            [iScheduleID] => 26
            [iAdminID] => 5
            [vStaff] => Ms. Timberly
            [vStudent] => Layla Shunnarah
            [iPositionID] => 4
        ),
        [5] => Array (
            [iScheduleID] => 27
            [iAdminID] => 5
            [vStaff] => Ms. Timberly
            [vStudent] => Haidi Sanchez
            [iPositionID] => 4
        )
    )
)

您可以看到:

情况1:有2次iPositionID = 0,因为它们的两个iAdminID相同,因此一个iPositionID应该为0,而另一个iPositionID应该为1。对于iAdminID 5,这是相同的。

案例2:现在,您可以看到iAdminID 3只有一次,因此其iPositionID为2,而iPositionID 3已被跳过。

预期结果:

$return = Array (
    [9:00] => Array (
        [0] => Array (
            [iScheduleID] => 13                    
            [iAdminID] => 4
            [vStaff] => Ms. Lily
            [vStudent] => Taylor Osborn
            [iPositionID] => 0
        ),
        [1] => Array (
            [iScheduleID] => 14
            [iAdminID] => 4
            [vStaff] => Ms. Lily
            [vStudent] => Smaran Meesala
            [iPositionID] => 1
        ),
        [2] => Array (
            [iScheduleID] => 1
            [iAdminID] => 3
            [vStaff] => Ms. Sanchita
            [vStudent] => Darian Newell
            [iPositionID] => 2
        ),     
        [4] => Array (
            [iScheduleID] => 26
            [iAdminID] => 5
            [vStaff] => Ms. Timberly
            [vStudent] => Layla Shunnarah
            [iPositionID] => 4
        ),
        [5] => Array (
            [iScheduleID] => 27
            [iAdminID] => 5
            [vStaff] => Ms. Timberly
            [vStudent] => Haidi Sanchez
            [iPositionID] => 5
        )
    )
)

这是我尝试的:

<?php    
$nine = $return['9:00'];
$total_nine = count($nine);
$t1['nine'] = $nine;

for ($i=0; $i < $total_nine; $i++) {
    $t = $t1['nine'][$i]['iAdminID'];

    if($t == 4) {
        $t1['nine'][$i]['iPositionID'] = 0;
    } else if($t == 3) {
        $t1['nine'][$i]['iPositionID'] = 2;
    } else if($t == 5) {
        $t1['nine'][$i]['iPositionID'] = 4;
    }
}    
?>
吉恩

这是您的解决方案

输入值

$return = array(
    '9:00' => array(
        array(
            'iScheduleID' => 13,
            'iAdminID' => 4,
            'vStaff' => 'Ms. Lily',
            'vStudent' => 'Taylor Osborn',
            'iPositionID' => 0
        ),
        array(
            'iScheduleID' => 14,
            'iAdminID' => 4,
            'vStaff' => 'Ms. Lily',
            'vStudent' => 'Smaran Meesala',
            'iPositionID' => 0
        ),
        array(
            'iScheduleID' => 1,
            'iAdminID' => 3,
            'vStaff' => 'Ms. Sanchita',
            'vStudent' => 'Darian Newell',
            'iPositionID' => 2
        ),
        array(
            'iScheduleID' => 26,
            'iAdminID' => 5,
            'vStaff' => 'Ms. Timberly',
            'vStudent' => 'Layla Shunnarah',
            'iPositionID' => 4
        ),
        array(
            'iScheduleID' => 27,
            'iAdminID' => 5,
            'vStaff' => 'Ms. Timberly',
            'vStudent' => 'Haidi Sanchez',
            'iPositionID' => 4
        )
    )
);

$position = array();//for add iPositionID
$new = array();
foreach($return['9:00'] as $row){

    if(in_array($row['iPositionID'],$position)) $row['iPositionID'] = ($row['iPositionID']+1);//Check current iPositionID is in $position array. If = Yes then +1 in current row['iPositionID']

    $position[] = $row['iPositionID']; //add $row['iPositionID'] value in $position array

    $new['9:00'][$row['iPositionID']] = $row; //new values in $new array
}
echo "<pre>";print_r($new);

输出量

Array
(
    [9:00] => Array
        (
            [0] => Array
                (
                    [iScheduleID] => 13
                    [iAdminID] => 4
                    [vStaff] => Ms. Lily
                    [vStudent] => Taylor Osborn
                    [iPositionID] => 0
                )

            [1] => Array
                (
                    [iScheduleID] => 14
                    [iAdminID] => 4
                    [vStaff] => Ms. Lily
                    [vStudent] => Smaran Meesala
                    [iPositionID] => 1
                )

            [2] => Array
                (
                    [iScheduleID] => 1
                    [iAdminID] => 3
                    [vStaff] => Ms. Sanchita
                    [vStudent] => Darian Newell
                    [iPositionID] => 2
                )

            [4] => Array
                (
                    [iScheduleID] => 26
                    [iAdminID] => 5
                    [vStaff] => Ms. Timberly
                    [vStudent] => Layla Shunnarah
                    [iPositionID] => 4
                )

            [5] => Array
                (
                    [iScheduleID] => 27
                    [iAdminID] => 5
                    [vStaff] => Ms. Timberly
                    [vStudent] => Haidi Sanchez
                    [iPositionID] => 5
                )

        )

)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

来自分类Dev

根据公共键/值对重新排列对象数组

来自分类Dev

根据JSON数组重新排列HTML表格数据

来自分类Dev

拆分数据并根据拆分号重新排列值

来自分类Dev

PHP根据id重新排列数据

来自分类Dev

重新排列NumPy数组中的值

来自分类Dev

根据嵌套数组重新排列对象数组

来自分类Dev

根据名称列表重新排列DataFrame列

来自分类Java

Java根据元素编号重新排列数组

来自分类Dev

JavaScript:根据与x的绝对差重新排列数组元素

来自分类Dev

根据每个对象的ID重新排列数组

来自分类Dev

Javascript-按数组重新排列对象数组(值)

来自分类Dev

在数组数组中重新排列值

来自分类Dev

R:根据排名重新排列数据框的列

来自分类Dev

r:根据时间重新排列数据帧

来自分类Dev

重新排列numpy数组

来自分类Dev

重新排列JavaScript数组

来自分类Dev

按列值重新排列numpy多级数组

来自分类Dev

用列值重新排列多维数组

来自分类Dev

重新排列行值

来自分类Dev

重新排列数据行

来自分类Dev

根据重新排列的重复片段(或如何删除重新排列的重复片段)切片熊猫数据框

来自分类Dev

如何在动态变化的d3 vis中根据其数据值重新排列svg元素的DOM顺序?

来自分类Dev

根据窗口宽度重新排列链

来自分类Dev

根据数字顺序重新排列数组元素,并以数组id表示

来自分类Dev

在cmd中重新排列数组元素

来自分类Dev

重新排列数组索引雄辩的Laravel

来自分类Dev

在Python中重新排列数组

来自分类Dev

排序和重新排列(不同的)数组

来自分类Dev

重新排列Python argparse参数组

TOP 榜单

热门标签

归档