duanchi4184 2014-06-23 07:20
浏览 49
已采纳

比较两个数组并附加最后一个值,如果它们是相同的其他值零

I have two string values namely $late_array and $wrong_array. The values are comma delimited. What I would like to do is compare the two arrays and if the first two elements are the same add the value to the end else make it zero. The arrays I have:

$late_array = array(
            [0] => 140610d,Richard,12
            [1] => 140610a,Dave,22
            [2] => 140610n,Noddy,121
            [3] => 140610a,Nick,15
 )
 $wrong_array = array(
            [0] => 140610d,Richard,2
            [1] => 140610d,Mary,60
            [2] => 140610a,Dave,11
            [3] => 140610n,Noddy,90
 )

The end result should be:

 $combined_array = array(
            [0] => 140610d,Richard,12,2
            [1] => 140610d,Mary,0,60
            [2] => 140610a,Dave,22,11
            [3] => 140610a,Nick,15,0
            [4] => 140610n,Noddy,121,90
  )

I have so far formed a foreach and used the '===' operators to check if the date and name match then output as I want but I have not been able to get it to work if the name is not present in one array but another to make the value zero.

EDIT: Just to clear it up, hopefully. If the value is present in both arrays then the date,name,late value,wrong value should show. But if the value is present in late only then the value for wrong should be 0, same visa versa. Added "Nick" to try and explain a bit better.

This is what I did to solve the problem so far:

$wrong_val = array();
foreach($out as $wrong_value) { 
    $wrong_tosearch[] = substr($wrong_value,0,strrpos($wrong_value,",")); 
    $w_id = substr($wrong_value,0,strrpos($wrong_value,","));
    $wrong_val[$w_id] = substr($wrong_value,strrpos($wrong_value,",")+1,strlen($wrong_value));
}
foreach($sql_late_array as $late_value) {
    $late_tosearch[] = substr($late_value,0,strrpos($late_value,","));
    $l_id = substr($late_value,0,strrpos($late_value,","));
    $late_val[$l_id] = substr($late_value,strrpos($late_value,",")+1,strlen($late_value));
}
$merge = array_merge($wrong_tosearch,$late_tosearch);
$sort = array_values(array_unique($merge));

$combined_array = array();
foreach ($sort as $search_val) {
    if (array_key_exists($search_val,$wrong_val) !== FALSE) {
        foreach ($wrong_val as $w_key=>$w_val) {
            $combined_array[$w_key]['late'] = "0";
            $combined_array[$w_key]['wrong'] = $w_val;
        }
    }
    if (array_key_exists($search_val,$late_val) !== FALSE) {
        foreach ($late_val as $l_key=>$l_val) {
            $combined_array[$l_key]['wrong'] = "0";
            $combined_array[$l_key]['late'] = $l_val;
        }
    }
}
print_r($combined_array);
  • 写回答

2条回答 默认 最新

  • doushi5024 2014-06-23 09:02
    关注

    Check if below code solves your problem.

    $late_array = array(
            "0" => "140610d,Richard,12",
            "1" => "140610a,Dave,22",
            "2" => "140610n,Noddy,121"
     );
        $wrong_array = array(
            "0" => "140610d,Richard,2",
            "1" => "140610d,Mary,60",
            "2" => "140610a,Dave,11",
            "3" => "140610n,Noddy,90"
     );
    
     foreach($wrong_array as $wv)
     { 
      $tosearch = substr($wv,0,strrpos($wv,",")-1); 
      $valtoadd = substr($wv,strrpos($wv,",")+1,strlen($wv)); 
    
      $added=false;
      foreach($late_array as $lv)
      {
         if(strstr($lv, $tosearch) !== false) 
         {  
            $combined_array[] = $lv.",".$valtoadd; 
            $added=true;
            break;
         }
      }
      if(!$added)
        $combined_array[] = $tosearch.",0,".$valtoadd; 
      }
    
      $tcombined_array = $combined_array; 
      foreach($late_array  as $wv)
      {
      $added = false;   
      foreach($tcombined_array  as $cv)
        if(strstr($cv,$wv)) 
         $added = true; 
    
      if(!$added) $combined_array[] = $wv.",0";
      } 
    
      print_r($combined_array);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?