按键和值比较两个数组

时间:2015-05-14 14:20:18

标签: php arrays

我有两个包含多个键和值的数组,但我希望最终数组比较第一个数组键和值,如果该值在第二个数组中出现,那么它将返回,否则取消设置所有索引数组值。

我的第一个数组

Array
(
    [0] => Array
        (
            [TXNID] => 225
        )

    [1] => Array
        (
            [TXNID] => 226
        )

)

第二个数组:

Array
(
    [0] => Array
            (
                [TXNID] => 224
                [TransactionName] => PAY-15-16-0031
                [TransactionDate] => 2015-05-12 00:00:00

            )

        [1] => Array
            (
                [TXNID] => 225
                [TransactionName] => RCT-15-16-0096
                [TransactionDate] => 2015-05-13 00:00:00

            )

        [2] => Array
            (
                [TXNID] => 226
                [TransactionName] => PAY-15-16-0032
                [TransactionDate] => 2015-05-13 00:00:00

            )

    )

最终阵列:

Array
(


        [0] => Array
            (
                [TXNID] => 225
                [TransactionName] => RCT-15-16-0096
                [TransactionDate] => 2015-05-13 00:00:00

            )

        [1] => Array
            (
                [TXNID] => 226
                [TransactionName] => PAY-15-16-0032
                [TransactionDate] => 2015-05-13 00:00:00

            )

    )

3 个答案:

答案 0 :(得分:1)

这将完成工作,但有两个嵌套循环不好

$output = array();
foreach ($first as $f) {
    foreach ($second as $s) {
        if ($s['TXNID'] == $f['TXNID']) {
            $output[] = $s;
        }
    }
}

最好事先重新组织输入:

$first = array(
    225,
    226
);

$second = array(
    224 => array(
        'TransactionName' => 'PAY-15-16-0031',
        'TransactionDate' => '2015-05-12 00:00:00'
    ),
    225 => array(
        'TransactionName' => 'RCT-15-16-0096',
        'TransactionDate' => '2015-05-13 00:00:00'
    ),
    226 => array(
        'TransactionName' => 'PAY-15-16-0032',
        'TransactionDate' => '2015-05-13 00:00:00'
    )
);

然后您可以迭代$first并直接将每个值用作$second的键,而无需第三个数组。

foreach ($first as $key) {
    // do something with $second[$key]
}

答案 1 :(得分:1)

//array1 is the first array you gave
//array2 the second
//array3 the result

function array_key_exists_r($needle, $haystack)
{
 $result = array_key_exists($needle, $haystack);
 if ($result) return $result;
 foreach ($haystack as $v) {
  if (is_array($v)) {
   $result = array_key_exists_r($needle, $v);
  }
  if ($result) return $result;
 }
 return $result;
}

foreach($array2 as $elem_array2)
{
    if(array_key_exists_r($elem_array2['TXNID'],$array1))
       $array3[] = $elem_array2;
}

答案 2 :(得分:0)

我考虑了一个简单的例子来解释其工作原理,

请参考以下内容。

$keys = array(
    "field1" => "first",
    "field2" => "second",
    "field3" => "third"
);
$values = array(
    "field1value" => "dinosaur",
    "field2value" => "pig",
    "field3value" => "platypus"
);

foreach ($keys as $arrval1) {
    $newarray1[$arrval1] = $arrval1;
}
$i = 0;
foreach ($values as $arrval2) {
    $j = 0;
    foreach ($newarray1 as $key => $val) {
        if ($i == $j) {
            $newarray1[$key] = $arrval2;
            break;
        }
        $j++;
    }
    $i++;
}
print_r($newarray1);