PHP IF ELSE条件和Foreach

时间:2014-10-14 10:46:12

标签: php arrays

我试图比较来自3个阵列的数据有一个小问题,其中一个是源,另外两个是条件。

情景是下一个:

$array1 = array('code' => '123', 'code' => '124', 'code' => '125', 'code' => '126', 'code' => '127');
$array2 = array(
    array('code1' => '123', 'country' => 'US', 'listed' => '0'), 
    array('code1' => '124', 'country' => 'US', 'listed' => '1'),
    array('code1' => '125', 'country' => 'US', 'listed' => '1')
);
$array3 = array(
    array('code2' => '123', 'country' => 'US', 'listed' => '1'),
    array('code2' => '126', 'country' => 'US', 'listed' => '0'),
    array('code2' => '127', 'country' => 'US', 'listed' => '1')
);

$final = array_merge($array1,$array2,$array3);

foreach ($final as $f) {
    if ($f['code'] == $f['code1']) {
        if ($f['listed'] > 0) {
            $finalListed = $f['listed'];
        }
    } elseif ($f['code'] == $f['code2']) {
        if ($f['listed'] > 0) {
            $finalListed = $f['listed'];
        }
    }

    $newFinalArray = array(
        'code' = $finalCode,
        'listed' = $finalListed,
        'country' = $finalCountry
    );
}

所以我需要的是首先检查来自$array1的代码是否存在于$array2中,如果$array2中的代码是否已列出,则检查$array3并且等等。

因此,如果$array2上存在代码,并且列出的是1更新数据库,如果不存在,则列出{更新数据库$array3如果存在,则列出的值为1更新,如果不更新{{1 }}

我的想法是$array2来自1个网站而$array2来自另一个网站,因此,如果不是1,那么第二个是{2}来自$array3

问题在于我无法对其进行排序,我已经尝试$array2但是这只组合了2个数组,并且参数需要完全正确。 使用数组合并,我将3个数组合并为一个,然后在foreach上,并在应用条件时,它表示变量未定义。

3 个答案:

答案 0 :(得分:2)

首先,我看到你的数组声明方式存在很多问题

前 -

$array1 = array('code' = > '123', 'code' = > '124', 'code' = > '125', 'code' = > '126', 'code' = > '127'); 

只是

$array1 = array('code' => '127'); //because of same index it will only consider the last value

但是我修改了你的数组并试图准备一个可能对你有用的解决方案。请在下面查看一次。

<?php
$array1 = array('123', '124', '125', '126', '127');
$array2 = array(array('code' => '123', 'country' => 'US', 'listed' => '0'),     array('code' => '124', 'country' => 'US', 'listed' => '1'), array('code' => '125', 'country' => 'US', 'listed' => '1'));
$array3 = array(array('code' => '123', 'country' => 'US', 'listed' => '1'), array('code' => '126', 'country' => 'US', 'listed' => '0'), array('code' => '127', 'country' => 'US', 'listed' => '1'));

function compareSitesAndUpdate($array1, $array2, $array3) {
    foreach($array1 as $code) {
    if(isCodeExistsInArray($code,$array2)) {
       echo $code . ' is in array2 and listed <br />';
    }
    else { // ;( Not in Array2 check in Array3
      echo $code . ' not listed in array2 - checking in array3 <br />';
      if(isCodeExistsInArray($code,$array3)) {
         echo $code . ' is in array3 and listed <br />';
      }
      else {
        echo $code . ' not listed in array3 also - do whatever you want to do <br />';
      }
    } 
    }
}

//Note $earray is always expected to be in the format of $array2/$array3
//And key of $array2 and $array3 should always be 'code' - Not necessary to change the keys are they are two diff arrays
function isCodeExistsInArray($ecode, $earray) {
    foreach($earray as $code_array) {
    if($ecode == $code_array['code']) { //code match found - now check if it is listed
       if($code_array['listed'] == 1) { //Got what we need - return TRUE and Break
        return TRUE;
       }
        }
    }

    return FALSE; //any other case return False;
}

compareSitesAndUpdate($array1, $array2, $array3);
?>

答案 1 :(得分:0)

为什么不尝试这样的事情:

foreach($array1 as $key => $code){
 if(in_array($code, $array2) {
  echo 'Value is in the array! :D ';
 } 
 else {
  if(in_array($code, $array3) {
    echo 'Value is in array3! :D';
  }
}

答案 2 :(得分:0)

假设两个数组中的代码具有相同的键:$array2$array3,即:

$array1 = array(
    '123', 
    '124', 
    '125', 
    '126', 
    '127'
);

$array2 = array(
    array('code' => '123', 'country' => 'US', 'listed' => '0'),
    array('code' => '124', 'country' => 'US', 'listed' => '1'),
    array('code' => '125', 'country' => 'US', 'listed' => '1')
);

$array3 = array(
    array('code' => '123', 'country' => 'US', 'listed' => '1'),
    array('code' => '126', 'country' => 'US', 'listed' => '0'),
    array('code' => '127', 'country' => 'US', 'listed' => '1')
);

然后:

$newFinalArray = array_filter(array_merge($array2, $array3), function($el){
    if($el['listed'] > 0 && in_array($el['code'], $GLOBALS['array1'])) {
        return true;
    }
});