0

我必须排列,我想将这两个数组与一个主键 (ID) 合并,我想按字母顺序排列这个 NEW 数组(姓氏字段)

Array ( 
[0] =>
 Array ( [id] => 172 
         [user_id] => 1217330 
         [behaviour_action_id] => 97 
         [state] => accepted 
     ) 
[1] =>
 Array ( 
    [id] => 173
    [user_id] => 1217330 
    [behaviour_action_id] => 97 
    [state] => pending 
     ) ) 

而这个数组

Array ( 
[1217330 ] => 
Array ( [firstname] => Philip 
        [lastname] => Audet
        [birthdate] => 1995-07-17 
        [id] => 1217330 
        ) 
[232323] => 
Array ( [firstname] => Frédéric 
        [lastname] => Bouchard-Dubé
        [birthdate] => 1995-07-17 
        [id] => 232323
        ) 

我想拥有这个

[0] =>
 Array ( [id] => 172 
         [user_id] => 1217330 
         [behaviour_action_id] => 97 
         [state] => accepted
         [firstname] => Philip 
         [lastname] => Audet 
     ) 
[1] =>
 Array ( 
    [id] => 173
    [user_id] => 1217330 
    [behaviour_action_id] => 97 
    [state] => pending 
    [firstname] => Philip 
    [lastname] => Audet 
     ) ) 

我不想有这个数组的生日索引,我只想从第二个数组中获取名字和姓氏。所以我想用 ID(第二个表)计算索引 USER_ID(first_table)。我还想用姓氏按字母顺序排列这个新表(在我的例子中,我不需要按字母顺序排序,但我需要这样做)

有人可以帮助我吗?谢谢谢谢

4

4 回答 4

1

这正在做你正在寻找的东西。由于您的示例与上下文无关,您可能需要采取一些不同的方法,但我希望它可以帮助您完成任务。

$behaviour = array(
    array(
        'id' => 172,
        'user_id' => 1217330,
        'behaviour_action_id' => 97,
        'state' => 'accepted'
    ),
    array(
        'id' => 173,
        'user_id' => 232323,
        'behaviour_action_id' => 97,
        'state' => 'pending'
    ),  
);



$users = array(
    1217330 => array(
        'firstname' => 'Philip',
        'lastname' => 'Audet',
        'birthdate' => '1995-07-17',
        'id' => 1217330
    ),
    232323 => array(
        'firstname' => 'Frédéric',
        'lastname' => 'Bouchard-Dubé',
        'birthdate' => '1995-07-17',
        'id' => 232323
    ),  
);


//we will collect the data in a new array
$combined = array();

//now we loop through all behaviours
foreach($behaviour as $key => $behaviourData){

    //we look up the data which belongs to the user of this behaviour
    $wantedUserData = $users[$behaviourData['user_id']];

    //birthdate is unwanted
    unset($wantedUserData['birthdate']);

    //merge data
    $combined[] = array_merge($behaviourData, $wantedUserData);
}

//order array
usort($combined,'cmp');

//voilà!
var_dump($combined);


//Comparison function used in usort above
function cmp($a, $b){
    if ($a['lastname'] == $b['lastname']){
        return 0;
    }
    return ($a['lastname'] < $b['lastname']) ? -1 : 1;
}
于 2012-06-26T14:11:21.730 回答
1
$arr1 = array (
    array("id"=>172, "user_id"=>1217330, "behaviour_action_id"=>97, "state"=>"accepted"),
    array("id"=>173, "user_id"=>1217330, "behaviour_action_id"=>97, "state"=>"pending")
);
$arr2 = array(
        "1217330" => array(
            "firstname" => "Philip",
            "lastname"  => "Audet",
            "birthdate" => "1995-07-17",
            "id"        => 1217330
        ),
        "232323" => array (
            "firstname" => "Frédéric",
            "lastname" => "Bouchard-Dubé",
            "birthdate" => "1995-07-17",
            "id" => 232323
        )
);
foreach($arr1 as $arr) {
    $extra = $arr2[$arr["user_id"]];
    unset($extra["birthdate"]);
    $newarray[] = array_merge($extra, $arr);
}

print_r($newarray);
于 2012-06-26T14:20:22.483 回答
0

好吧,我不想给出一个熟化的代码,但这个算法可以完成这项工作:

1) 遍历第一个数组并创建另一个以 user_id 为键的数组。像

$new_array[$array['user_id']] = array($array['id'], $array['state']...);

2)循环遍历第二个数组和unset()key birthdate

3)使用array_merge_recursive()并合并两个数组。

于 2012-06-26T14:09:46.453 回答
0

这个?(假设$arr1$arr2给定数组)

$arr3 = array();
foreach($arr1 as $key=>$val) {
   $arr3[$key] = $val;
   $arr3[$key]["firstname"] = $arr2[$val["user_id"]]["firstname"];
   $arr3[$key]["lastname"] = $arr2[$val["user_id"]]["lastname"];
}

echo "<pre>";
print_r($arr3);
echo "</pre>";
于 2012-06-26T14:21:52.207 回答