这是一个数组:
$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: 有两次iPositionID = 0,因为它们都有iAdminID相同,所以一个iPositionID应为0,另一个应为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;
}
}
?>
答案 0 :(得分:2)
假设iAdminID
的值始终为4, 3 or 5
。您可以先使用相应的起始值设置位置数组,然后在每次循环迭代中递增
$nine = $return['9:00'];
$total_nine = count($nine);
$t1['nine'] = $nine;
$position = array(4 => 0, 3 => 2, 5 => 4); // set position array
for ($i=0; $i < $total_nine; $i++) {
$t = $t1['nine'][$i]['iAdminID'];
if($t == 4) {
$t1['nine'][$i]['iPositionID'] = $position[$t];
} else if($t == 3) {
$t1['nine'][$i]['iPositionID'] = $position[$t];
} else if($t == 5) {
$t1['nine'][$i]['iPositionID'] = $position[$t];
}
$position[$t]++;
}
print_r($t1);
答案 1 :(得分:1)
这是您的解决方案
<强>输入
$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
)
)
)