循环遍历数组并更改特定索引

时间:2016-03-11 21:08:07

标签: php arrays

我正在尝试在检查另一个数组的值时更改一个数组的值。我有一个数组$ arr,其值将被检查。这是:

$arr = array("Saturday", "Sunday");

$ checkArr是一个包含7个值的数组,作为" 0",我假设每个数字是一个工作日。这是:

$checkArr = array(0,0,0,0,0,0,0);

我想要实现的是,我将使用for循环检查$ arr的每个值,如果特定索引处的值与特定日期匹配,则更改" $ checkArr"的值。在特定指数。在下面的代码中,我使用的是代码计数($ arr),因为实际上我有两个$ arr数组;

代码:

 for($i= 0; $i < count($arr); $i++){ 
                    $checkArr = array(0,0,0,0,0,0,0);
                    if($arr[$i] == "Monday" ){ $checkArr[0] = 1; }
                    elseif($arr[$i] == "Tuesday" ){ $checkArr[1] = 1; }
                    elseif($arr[$i] == "Wednesday"){ $checkArr[2] = 1; }
                    elseif($arr[$i] == "Thursday"){ $checkArr[3] = 1; } 
                    elseif($arr[$i] == "Friday"){ $checkArr[4] = 1; } 
                        elseif($arr[$i] == "Saturday" ){  $checkArr[5] = 1;  } 
                    elseif($arr[$i] == "Sunday" ){ $checkArr[6] = 1; } 

                }
                print_r($checkArr);

原始代码:

  foreach($officeDetails as $key=>$value){ 
                $str = $value['days'];
                $arr = explode(",", $str);

                $checkArr = array(0,0,0,0,0,0,0);
                for($i= 0; $i < count($arr); $i++){

                    if($arr[$i] == "Monday" ){ $checkArr[0] == 1; }
                    elseif($arr[$i] == "Tuesday" ){ $checkArr[1] = 1; }
                    elseif($arr[$i] == "Wednesday"){ $checkArr[2] = 1; }
                    elseif($arr[$i] == "Thursday"){ $checkArr[3] = 1; } 
                    elseif($arr[$i] == "Friday"){ $checkArr[4] = 1; } 
                        elseif($arr[$i] == "Saturday" ){$checkArr[5] == 1; } 
                    elseif($arr[$i] == "Sunday" ){ $checkArr[6] = 1; } 
                    print_r($checkArr);
                }
   } 

但问题是,它没有改变$ checkArr特定索引的值。我知道我在这里遗漏了一些东西。有人可以帮忙吗?我将非常感激。

亲切的问候

3 个答案:

答案 0 :(得分:2)

你有一行:$ checkArr = array(0,0,0,0,0,0,0);在for循环中,所以每次都重置它。看看下面的代码,让数组在for循环之外,使它工作。

代码:

$arr = array("Saturday", "Sunday");
$checkArr = array(0,0,0,0,0,0,0);
for($i= 0; $i < count($arr); $i++){
  if($arr[$i] == "Monday" ){ $checkArr[0] = 1; }
  elseif($arr[$i] == "Tuesday" ){ $checkArr[1] = 1; }
  elseif($arr[$i] == "Wednesday"){ $checkArr[2] = 1; }
  elseif($arr[$i] == "Thursday"){ $checkArr[3] = 1; }
  elseif($arr[$i] == "Friday"){ $checkArr[4] = 1; }
  elseif($arr[$i] == "Saturday" ){  $checkArr[5] = 1;  }
  elseif($arr[$i] == "Sunday" ){ $checkArr[6] = 1; }
}

print_r($checkArr);

原始代码:

$checkArr = [];
foreach($officeDetails as $key=>$value){ 
  $str = $value['days'];
  $arr = explode(",", $str);
  $checkArr[$key] = array(0,0,0,0,0,0,0);

  for($i= 0; $i < count($arr); $i++){
    if($arr[$i] == "Monday" ){ $checkArr[$key][0] == 1; }
    elseif($arr[$i] == "Tuesday" ){ $checkArr[$key][1] = 1; }
    elseif($arr[$i] == "Wednesday"){ $checkArr[$key][2] = 1; }
    elseif($arr[$i] == "Thursday"){ $checkArr[$key][3] = 1; } 
    elseif($arr[$i] == "Friday"){ $checkArr[$key][4] = 1; } 
    elseif($arr[$i] == "Saturday" ){$checkArr[$key][5] == 1; } 
    elseif($arr[$i] == "Sunday" ){ $checkArr[$key][6] = 1; } 
  }
}

print_r($checkArr);

答案 1 :(得分:0)

您已在for循环中重置$ checkArr。

<?php
$arr = array("Saturday", "Sunday");
$checkArr = array(0,0,0,0,0,0,0);
foreach($arr as $val){ 
                if($val == "Monday" ){ $checkArr[0] = 1; }
                elseif($val == "Tuesday" ){ $checkArr[1] = 1; }
                elseif($val  == "Wednesday"){ $checkArr[2] = 1; }
                elseif($val  == "Thursday"){ $checkArr[3] = 1; } 
                elseif($val  == "Friday"){ $checkArr[4] = 1; } 
                elseif($val == "Saturday" ){  $checkArr[5] = 1;  } 
                elseif($val == "Sunday" ){ $checkArr[6] = 1; } 

}
print_r($checkArr);
?>

答案 2 :(得分:0)

这是完全不同的方法。看看并测试它是否给出你期望的输出。

$officeDetails[0]['days'] = "Saturday,Sunday";
$officeDetails[1]['days'] = "Monday,Friday";
foreach($officeDetails as $key=>$value){
    $checkArr = array(0,0,0,0,0,0,0);
    $str = $value['days'];
    if (strpos($str, 'Monday') !== false) { $checkArr[0] = 1;}
    if(strpos($str, 'Tuesday') !== false ){ $checkArr[1] = 1; }
    if(strpos($str, 'Wednesday') !== false){ $checkArr[2] = 1; }
    if(strpos($str, 'Thursday') !== false){ $checkArr[3] = 1; }
    if(strpos($str, 'Friday') !== false){ $checkArr[4] = 1; }
    if(strpos($str, 'Saturday') !== false){$checkArr[5] = 1; }
    if(strpos($str, 'Sunday') !== false ){ $checkArr[6] = 1; }
    print_r($checkArr);
}

输出:

Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 1
[6] => 1
)
Array
(
[0] => 1
[1] => 0
[2] => 0
[3] => 0
[4] => 1
[5] => 0
[6] => 0
)