在某些数组中查找类似的子数组

时间:2013-11-15 16:25:35

标签: php arrays algorithm

有一些数组:

$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

2 个答案:

答案 0 :(得分: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
)

答案 1 :(得分: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
        )

)