2013-11-15 45 views
0

有一些數組:尋找類似的子陣列在某些陣列

$a = array(
    0 => array('value' => 1000, 'name' => 'AA1'), 
    1 => array('value' => 2000, 'name' => 'AA2'), 
    2 => array('value' => 3000, 'name' => 'AA3'), 
    3 => array('value' => 4000, 'name' => 'AA4'), 
); 
$b = array(
    0 => array('value' => 1000, 'name' => 'BB1'), 
    1 => array('value' => 3000, 'name' => 'BB2'), 
    2 => array('value' => 1700, 'name' => 'BB3'), 
    3 => array('value' => 1200, 'name' => 'BB4'), 
); 
$c = array(
    0 => array('value' => 3000, 'name' => 'CC1'), 
    1 => array('value' => 4000, 'name' => 'CC2'), 
    2 => array('value' => 4300, 'name' => 'CC3'), 
    3 => array('value' => 5000, 'name' => 'CC4'), 
); 

如何創建數組$ A,$ B和$ C具有相同變種一個新的陣列?和「價值」字段順序呢..

$d = array(
    0 => array('value' => 3000, 'name' => 'AA3'); 
); 

真實的例子:http://3v4l.org/MAWjd

+1

我別 瞭解你的輸出的標準是什麼。你能解釋得更好嗎? –

+0

需要找到所有具有相似「值」的子庫 – brigantaggior

+0

如果該數組應具有所有具有相同值的元素,那麼如何「排序」它們? – aleation

回答

0
// Test Data 
$timeVariantSerialized = 'a:3:{s:7:"group_a";a:3:{i:0;a:3:{s:10:"id_variant";i:122;s:4:"name";s:13:"3 hour 30 min";s:4:"time";i:12600;}i:1;a:3:{s:10:"id_variant";i:173;s:4:"name";s:6:"3 hour";s:4:"time";i:10800;}i:2;a:3:{s:10:"id_variant";i:271;s:4:"name";s:6:"1 hour";s:4:"time";i:3600;}}s:7:"group_b";a:2:{i:0;a:3:{s:10:"id_variant";i:107;s:4:"name";s:13:"1 hour 30 min";s:4:"time";i:5400;}i:1;a:3:{s:10:"id_variant";i:321;s:4:"name";s:6:"3 hour";s:4:"time";i:10800;}}s:7:"group_c";a:4:{i:0;a:3:{s:10:"id_variant";i:28;s:4:"name";s:6:"1 hour";s:4:"time";i:3600;}i:1;a:3:{s:10:"id_variant";i:98;s:4:"name";s:6:"3 hour";s:4:"time";i:10800;}i:2;a:3:{s:10:"id_variant";i:157;s:4:"name";s:6:"2 hour";s:4:"time";i:7200;}i:3;a:3:{s:10:"id_variant";i:158;s:4:"name";s:13:"1 hour 30 min";s:4:"time";i:5400;}}}'; 

// Test Data Array 
$time_variant = unserialize($timeVariantSerialized); 

$groupsCount = count($time_variant); 

foreach($time_variant as $groupId => $groupArray) 
{ 
    foreach($groupArray as $groupChildId => $groupChildArray) 
    { 
     $timeCountsArray[$groupChildArray['time']][$groupId] = $groupChildId; 
    } 
} 

$result = array(); 

foreach($timeCountsArray as $time => $groupIdsArray) 
{ 
    if($groupsCount > count($groupIdsArray)) continue; 

    foreach($groupIdsArray as $groupId => $groupChildId) 
    { 
     $result[$time_variant[$groupId][$groupChildId]['id_variant']] 
      = $time_variant[$groupId][$groupChildId]; 
    // If you need only one result , uncomment the below break 
    // break; 
    } 
    // If more than 1 time could have variants , comment out below break to get them all 
    break; 
} 

ksort($result); 

print_r($result); 

結果數組:

array 
(
    '98' => array 
     (
      'id_variant' => 98 
      'name' => 3 hour 
      'time' => 10800 
     ) 

    '173' => array 
     (
      'id_variant' => 173 
      'name' => 3 hour 
      'time' => 10800 
     ) 

    '321' => array 
     (
      'id_variant' => 321 
      'name' => 3 hour 
      'time' => 10800 
     ) 

) 
+0

太棒了!謝謝.. – brigantaggior

0

我不知道這是否是你想要的。

結果是一個單一的陣列,以在給定陣列中的所有重複值:

//Lets merge them into a big array to work in a easier way; 
//It will be a single level array as follow: [a0] => 1000, [a1] => 2000 etc. 
$array = array('a' => $a, 'b' => $b, 'c' =>$c); 
foreach($array as $letter => $subArray){ 
    foreach($subArray as $key => $values){ 
     $newArray[$letter.$key] = $values['value']; 
    } 
} 

$uniques = array_unique($newArray); //getting the non-repeated values 
$repeated = array_diff_assoc($newArray, $uniques); // stripping them out 
$final = array_unique($repeated); //cleaning the repeated values to just 1 of each 

print_r($final); 

結果

Array 
(
    [b0] => 1000 
    [b1] => 3000 
    [c1] => 4000 
) 
+0

這不是正確的答案,因爲我沒有在數組$ c中賦值'1000',而在數組$ b中不存在數值'4000'。 – brigantaggior