2015-06-01 96 views
1

你好,我想结合两个php数组。结合两个数组在php

首先一个

array (size=13) 
    0 => 
    object(stdClass)[30] 
     public 'ID' => string '1' (length=1) 
     public 'name' => string 'html5' (length=5) 
     public 'img' => string 'HTML5.png' (length=9) 
    1 => 
    object(stdClass)[31] 
     public 'ID' => string '2' (length=1) 
     public 'name' => string 'css3' (length=4) 
     public 'img' => string 'css.png' (length=7) 
    2 => 
    object(stdClass)[32] 
     public 'ID' => string '3' (length=1) 
     public 'name' => string 'php' (length=3) 
     public 'img' => string 'php1.png' (length=8) 
    3 => 
    object(stdClass)[33] 
     public 'ID' => string '4' (length=1) 
     public 'name' => string 'java script' (length=11) 
     public 'img' => string 'javascript.png' (length=14) 

第二个

array (size=3) 
    0 => 
    object(stdClass)[26] 
     public 'ID' => string '1' (length=1) 
     public 'IDuser' => string '1' (length=1) 
     public 'IDskill' => string '1' (length=1) 
    1 => 
    object(stdClass)[27] 
     public 'ID' => string '2' (length=1) 
     public 'IDuser' => string '1' (length=1) 
     public 'IDskill' => string '3' (length=1) 
    2 => 
    object(stdClass)[28] 
     public 'ID' => string '3' (length=1) 
     public 'IDuser' => string '1' (length=1) 
     public 'IDskill' => string '4' (length=1) 

从第一阵列ID是从第二阵列等于IDskill。我想结合起来,创造新的数组,如果IDskill和ID是一样的,像这样的东西在新的数组

  public 'ID' => string '1' (length=1) 
      public 'name' => string 'html5' (length=5) 
      public 'img' => string 'HTML5.png' (length=9) 
    ===>New field public 'MATCH' => string '1' (length=9) 

回答

0

试试这个(其中a1是第一阵列和a2是第二个和$result是最终的合并阵列):

$result = array(); 
$n=0; 
foreach($a1 as $k=>$v) { 
    if (isset($v->ID) && isset($a2[$n]->ID) && $v->ID==$a2[$n]->ID) { 
     $result[$n]=$v; 
     $result[$n]->MATCH=1; 
    } 
    $n++; 
} 
print_r($result); 
+0

接近,但我这里有严重性:通知 消息:未定义偏移量:12 –

+0

我用'isset'检查更新了我的答案。通知您正在获取可能是因为阵列具有不同的大小。 –

0

使用array_merge()函数

<?php 
 
$a1=array("red","green"); 
 
$a2=array("blue","yellow"); 
 
print_r(array_merge($a1,$a2)); 
 
?>

<?php 
 
$fname=array("Peter","Ben","Joe"); 
 
$age=array("35","37","43"); 
 

 
$c=array_combine($fname,$age); 
 
print_r($c); 
 
?>

+0

这是一个例子,如何组合或合并两个arryas。 –

0

你可以使用array_mapanonymous functions

<?php 

$first_array = array(...); 
$second_array = array(...); 

// Anonymous function to extract the IDskills from the second array: 
$get_IDs = function($element) { 
    return($element->IDskill); 
}; 

// Array of (just) IDskills of the second array: 
$IDs = array_map($get_IDs, array_filter($second_array, function($element) { 
    // Anonymous function to filter $second_array elements without IDskill: 
    return(isset($element->IDskill)); 
})); 

// Another anonymous function that returns a new array with elements from 
// $first_array with the new MATCH property. 
// use(&$IDs) makes $IDs available inside the function scope: 
$check_match = function($element) use(&$IDs) { 

    // We use clone because we don't want to modify the original array values: 
    $match_element = clone $element; 

    if(isset($match_element->ID)) 
     $match_element->MATCH = in_array($match_element->ID, $IDs); 
    else 
     $match_element->MATCH = false; 

    return($match_element); 
}; 

$match = array_map($check_match, $first_array); 

?> 

如果你不想使用两个第一匿名函数或不想创建$IDs阵列,您可以将替换为use(&$second_array)和线:

$match_element->MATCH = in_array($match_element->ID, $IDs); 

为一个循环,通过$second_array元件迭代来检查它们中的任何是否具有IDskill等于$match_element->ID

此外,如果数组中的所有元素具有相同的属性,则可能不需要检查IDskillID是否存在。

0

让我补充我的两分钱,如何找到从1个数组元素匹配的ID在第二个

$tmp = array_filter($array2, function($ar2) use ($ID) { return $ar2->IDskill === $ID; }); 
    if ($tmp) // Found 
    else // Not found 

所有代码可以是

result = array() 
    foreach ($array1 as $item) { 
     $ID = $item->ID; 
     $tmp = array_filter($array2, function($ar2) use ($ID) { return $ar2->IDskill === $ID; }); 
     if ($tmp) { // Found 
      $tmp = $item; 
      $tmp->MATCH = 1; 
      result[] = $tmp; 
     } 
    } 
+0

@PatrikDupeliz为什么你拒绝接受?有什么不行吗? – splash58