在PHP中比较数组的键

时间:2016-03-21 07:04:26

标签: php arrays

这是我的第一个数组

$array1 = [
        'A' => 'Apple',
        'B' => 'Ball',
        'C' => 'Cat',
        'E' => 'Eagle',
        'F' => 'Fan'
    ];

第二阵列

$array2 = [
        'A' => 'apple is a fruit',
        'B' => 'ball is used to play',
        'C' => 'cat is an animal',
        'D' => '',
        'E' => 'eagle is a bird',
        'F' => ''
    ];

当前输出:

Array
(
    [Apple] => apple is a fruit
    [Ball] => ball is used to play
    [Cat] => cat is an animal
    [Eagle] => eagle is a bird
    [Fan] => 
)

预期产出:

Array
    (
        [Apple] => apple is a fruit
        [Ball] => ball is used to play
        [Cat] => cat is an animal
        [Eagle] => eagle is a bird
    )

我试过这样的

$arr4 = [];
if ($arr3 = array_intersect_key($array1, $array2)) {
    foreach ($arr3 as $k => $v) {
        $arr4[$v] = $array2[$k];
    }
}

print_r($arr4);

请帮助,提前致谢! 如果你看到当前的输出,我得到的Fan没有任何价值。我需要获得具有预期输出值的结果

5 个答案:

答案 0 :(得分:4)

试试这个:

<?php

$array1 = [
        'A' => 'Apple',
        'B' => 'Ball',
        'C' => 'Cat',
        'E' => 'Eagle',
        'F' => 'Fan'
    ];


$array2 = [
        'A' => 'apple is a fruit',
        'B' => 'ball is used to play',
        'C' => 'cat is an animal',
        'D' => '',
        'E' => 'eagle is a bird',
        'F' => ''
    ];


$result = [];

foreach($array2 as $key => $value)
{
    if(!empty($value) && isset($array1[$key]))
        $result[$array1[$key]] = $value;
}


echo '<pre>';
print_r($result);
echo '</pre>';

输出:

Array
(
    [Apple] => apple is a fruit
    [Ball] => ball is used to play
    [Cat] => cat is an animal
    [Eagle] => eagle is a bird
)

答案 1 :(得分:1)

您可以使用array_filter()功能删除空结果:

document.querySelector('[ng-attr-id]')

<强>结果:

<?php
$array1 = array(
        'A' => 'Apple',
        'B' => 'Ball',
        'C' => 'Cat',
        'E' => 'Eagle',
        'F' => 'Fan'
    );
$array2 = array(
        'A' => 'apple is a fruit',
        'B' => 'ball is used to play',
        'C' => 'cat is an animal',
        'D' => '',
        'E' => 'eagle is a bird',
        'F' => ''
    );

$arr4 = array();

if ($arr3 = array_intersect_key($array1, $array2)) {
  foreach ($arr3 as $k => $v) {
    $arr4[$v] = $array2[$k];
  }
}
$removedEmpty = array_filter($arr4);
echo "<pre>";
print_r($removedEmpty);
?>

答案 2 :(得分:0)

你可以试试这个

$array3 = [];
foreach ($array1 as $key => $value) {
    if ($array2[$key] != '') {
        $array3[$value] = $array2[$key];
    }
}

echo '<pre>';
print_r($array3);

答案 3 :(得分:0)

IF

中添加foreach
if ($arr3 = array_intersect_key($array1, $array2)) {
        foreach ($arr3 as $k => $v) {
           if($array2[$k]){
             $arr4[$v] = $array2[$k];
           }
        }
    }

答案 4 :(得分:0)

使用array_filterarray_intersect_keyarray_combine函数的简短解决方案:

$array2 = array_filter($array2);

var_dump(array_combine(array_intersect_key($array1,$array2), $array2));

输出:

array(4) {
  ["Apple"]=>
  string(16) "apple is a fruit"
  ["Ball"]=>
  string(20) "ball is used to play"
  ["Cat"]=>
  string(16) "cat is an animal"
  ["Eagle"]=>
  string(15) "eagle is a bird"
}