Gireesh Doddipalli
Gireesh Doddipalli

Reputation: 490

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. I need to get the results which are having values like the expected output

Upvotes: 1

Views: 128

Answers (5)

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

Short solution with array_filter, array_intersect_key and array_combine function:

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

Upvotes: 0

guri
guri

Reputation: 684

Add IF inside foreach

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

Upvotes: 0

devpro
devpro

Reputation: 16117

You can use array_filter() function for removing empty result:

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

Upvotes: 1

aslawin
aslawin

Reputation: 1981

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
)

Upvotes: 4

Vasil Rashkov
Vasil Rashkov

Reputation: 1830

you can try this

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

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

Upvotes: 0

Related Questions