简体   繁体   English

比较PHP中数组的键

[英]Comparing keys of an array in PHP

Here is my First Array 这是我的第一个数组

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

Second Array 第二阵列

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

Current Output: 电流输出:

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

Expected Output: 预期产量:

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

I have tried like this 我已经尝试过这样

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

print_r($arr4);

Please help, Thanks in advance! 请帮助,在此先谢谢! If you see the current output, I am getting the result of Fan which has no value. 如果看到当前输出,我得到的是Fan的结果,它没有任何价值。 I need to get the results which are having values like the expected output 我需要获取具有预期输出值的结果

Try this: 尝试这个:

<?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>';

Output: 输出:

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

You can use array_filter() function for removing empty result: 您可以使用array_filter()函数删除空结果:

<?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);
?>

Result: 结果:

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

you can try this 你可以试试这个

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

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

Add IF inside foreach foreach添加IF

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

Short solution with array_filter , array_intersect_key and array_combine function: 简短的解决方案与array_filterarray_intersect_keyarray_combine功能:

$array2 = array_filter($array2);

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

The output: 输出:

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"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM