2016-03-21 8 views
1

Hier ist meine erste ArrayVergleich Schlüssel eines Arrays in PHP

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

zweiten 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' => '' 
    ]; 

Stromausgang:

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

Erwartete Ausgabe:

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

Ich habe versucht, wie dieser

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

print_r($arr4); 

Bitte helfen, Vielen Dank im Voraus! Wenn Sie die aktuelle Ausgabe sehen, bekomme ich das Ergebnis von Fan, das keinen Wert hat. Ich brauche die Ergebnisse zu erhalten, die Werte wie die erwartete Ausgabe haben

+0

Der gesamte Code ist in Ordnung Gebrauch 'array_remove ($ arr4)' auf leere Werte aus dem Feld zu entfernen; – itzmukeshy7

Antwort

0

Sie dieses

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

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

dieses Versuchen ausprobieren können:

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

Ausgang:

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

Abstimmung für optimierte Lösung – devpro

1

Sie können Verwenden Sie array_filter() Funktion zum Entfernen leeres Ergebnis:

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

Ergebnis:

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

hinzufügen IF innen foreach

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

Short Lösung mit array_filter, array_intersect_key und array_combine Funktion:

$array2 = array_filter($array2); 

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

Der Ausgang:

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