合并和排序关联数组

时间:2015-05-14 19:37:08

标签: php arrays sorting multidimensional-array

第一个阵列:

Array ( 
    [0] => Array ( [id] => 1 [occ] => 14 ) 
    [1] => Array ( [id] => 2 [occ] => 12 ) 
    [2] => Array ( [id] => 4 [occ] => 2 ) 
)

第二阵列:

Array ( 
    [0] => Array ( [id] => 1 [company_name] => Google   [CEO] => Mike ) 
    [1] => Array ( [id] => 2 [company_name] => Apple    [CEO] => Jones) 
    [2] => Array ( [id] => 2 [company_name] => Bmw      [CEO] => Steve) 
    [3] => Array ( [id] => 3 [company_name] => Hardsoft [CEO] => Lucy ) 
    [4] => Array ( [id] => 4 [company_name] => Lays     [CEO] => Morty) 
)

我想将这些数组合并为:

Array ( 
    [0] => Array ( [id] => 1 [company_name] => Google   [CEO] => Mike   [occ] => 14) 
    [1] => Array ( [id] => 2 [company_name] => Apple    [CEO] => Jones  [occ] => 12) 
    [2] => Array ( [id] => 3 [company_name] => Bmw      [CEO] => Steve  [occ] => 0) 
    [3] => Array ( [id] => 4 [company_name] => Hardsoft [CEO] => Lucy   [occ] => 2) 
    [4] => Array ( [id] => 5 [company_name] => Lays     [CEO] => Morty  [occ] => 0) 
)

然后按occ 对它们进行排序,以便Google成为第一,Apple排名第二,Hardsoft排名第三等等。

我如何存档?

4 个答案:

答案 0 :(得分:2)

这应该适合你:

首先,我使用array_column()将第一个数组中id's的数组作为“查找表”。然后我用array_values()重新索引第二个数组。

在此之后,我开始循环遍历第二个数组并重新分配id,只需使用innerArray + 1中的键。这样你就没有重复的id了。

然后我看array_search()如果新的id在查找表中,如果是,我将值occ从第一个数组分配给第二个数组。如果不是,我只需将0指定为第二个数组中的键occ的值。

完成所有操作后,使用usort()进行简单排序,比较occ的值并按其值排序(注意:如果要更改{{1}的排序转到DESC只需在ASC来电中将<更改为>即可。

usort()

输出:

<?php

    $ids = array_column($arr1, "id");
    $arr2 = array_values($arr2);

    foreach($arr2 as $k => &$v) {
        $v["id"] = ($k+1);
        if(in_array($v["id"], $ids))
            $arr2[$k]["occ"] = $arr1[array_search($v["id"], $ids)]["occ"];
        else
            $arr2[$k]["occ"] = 0;
    }           

    usort($arr2, function($a, $b){
        if($a["occ"] == $b["occ"])
            return 0;
        return $a["occ"] < $b["occ"] ? 1 : -1;
    });

    print_r($arr2);

?>

答案 1 :(得分:1)

首先将你的第一个数组改为一个关联的数组,其中id是键 - 所以最基本的方法只是一个简单的循环

$lookup_array = [];    
foreach ($firstarray as $row) {
   $lookup_array[$row['id']] = $row['occ'];
}

然后你可以循环遍历第二个数组并添加你需要的东西

foreach ($second_array as &$row) {
  $id = $row['id'];
  $occ = isset($lookup_array[$id]) ? $lookup_array[$id] : 0;
  $row['occ'] = $occ;
}

注意&amp;指向使$ row成为对数组元素的引用。

祝你好运

答案 2 :(得分:0)

你可以通过像这样的迭代来做到这一点

$first_array=Array ( 
 [0] => Array ( [id] => 1 [occ] => 14 ) 
 [1] => Array ( [id] => 2 [occ] => 12 ) 
 [2] => Array ( [id] => 4 [occ] => 2 ) 
);

$second_array=Array ( 
 [0] => Array ( [id] => 1 [company_name] => Google   [CEO] => Mike ) 
 [1] => Array ( [id] => 2 [company_name] => Apple    [CEO] => Jones) 
 [2] => Array ( [id] => 2 [company_name] => Bmw      [CEO] => Steve) 
 [3] => Array ( [id] => 3 [company_name] => Hardsoft [CEO] => Lucy ) 
 [4] => Array ( [id] => 4 [company_name] => Lays     [CEO] => Morty) 
)

$third_array=$second_array;

for($i=0;$i<sizeof($second_array);$i++)
{
    for($j=0;$j<sizeof($first_array);$j++)
    {
        if($second_array[$i][id]==$first_array[$j][id])
        {
            $third_array[$i][occ]=$first_array[$j][occ];
        }
    }    
    if(!isset($third_array[$i][occ]))  
    {
        $third_array[$i][occ]=0;
    }
}

现在通过调试

进行检查
var_dump($third_array);

可能你会看到类似这样的东西

Array ( 
 [0] => Array ( [id] => 1 [company_name] => Google   [CEO] => Mike   [occ] => 14) 
 [1] => Array ( [id] => 2 [company_name] => Apple    [CEO] => Jones  [occ] => 12) 
 [2] => Array ( [id] => 3 [company_name] => Bmw      [CEO] => Steve  [occ] => 0) 
 [3] => Array ( [id] => 4 [company_name] => Hardsoft [CEO] => Lucy   [occ] => 2) 
 [4] => Array ( [id] => 5 [company_name] => Lays     [CEO] => Morty  [occ] => 0) 
)

我希望这会对你有所帮助

答案 3 :(得分:0)

以下是完整的一个解决方案以及排序:)

$a = array ( 
 0 => array ( 'id' => 1, 'occ' => 14 ),  
 1 => array ( 'id' => 2, 'occ' => 12 ),  
 2 => array ( 'id' => 4, 'occ' => 2 ),  
);

$b = array ( 
'0' => array ( 'id' => 1, 'company_name' => 'Google',   'CEO' => 'Mike' ),  
'1' => array ( 'id' => 2, 'company_name' => 'Apple',    'CEO' => 'Jones'), 
'2' => array ( 'id' => 2, 'company_name' => 'Bmw',      'CEO' => 'Steve'), 
'3' => array ( 'id' => 3, 'company_name' => 'Hardsoft', 'CEO' => 'Lucy' ),  
'4' => array ( 'id' => 4, 'company_name' => 'Lays',     'CEO' => 'Morty'), 
);


$result = []; 
foreach($b as $b_key => $b_val) {
    $merge_array = getSubArrayOnId($b_val['id'], $a);//get portion to be merged from $a
    if(!empty($merge_array))
        $result[] = $b_val + $merge_array;
    else
        $result[] = $b_val + ['occ' => 0]; 
}

//now sorting using user defined function
usort($result, 'cmpOcc');

function getSubArrayOnId($id, $array)
{
    foreach ($array as $key=>$val) {
        if($val['id'] == $id) {
            return $array[$key];//return the actual sub-array
        }
    }
    return [];//if nothing found return empty array
}

function cmpOcc($a, $b) {
  return $b['occ'] - $a['occ'];
}

现在使用print_r我们将得到以下结果。

echo "<pre>";
print_r($result);

输出(已排序):

Array
(
    [0] => Array
        (
            [id] => 1
            [company_name] => Google
            [CEO] => Mike
            [occ] => 14
        )

    [1] => Array
        (
            [id] => 2
            [company_name] => Bmw
            [CEO] => Steve
            [occ] => 12
        )

    [2] => Array
        (
            [id] => 2
            [company_name] => Apple
            [CEO] => Jones
            [occ] => 12
        )

    [3] => Array
        (
            [id] => 4
            [company_name] => Lays
            [CEO] => Morty
            [occ] => 2
        )

    [4] => Array
        (
            [id] => 3
            [company_name] => Hardsoft
            [CEO] => Lucy
            [occ] => 0
        )

)