1

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

1
  • All code is fine use array_remove($arr4) to remove empty values from array; Commented Mar 21, 2016 at 7:20

5 Answers 5

4

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
)
Sign up to request clarification or add additional context in comments.

Comments

1

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
)

Comments

0

you can try this

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

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

Comments

0

Add IF inside foreach

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.