在 php 中合并我的 2 个数组

问题描述 投票:0回答:3

我有一个数组:

Array
(
    [0] => stdClass Object
        (
            [driver_id] => 2
            [total] => 308
            [total_driver] => 5
            [score1] => 61.6000
            [score2] => 62
        )

    [1] => stdClass Object
        (
            [driver_id] => 3
            [total] => 354
            [total_driver] => 5
            [score1] => 70.8000
            [score2] => 71
        )
)

还有另一个数组:

Array
(
    [0] => Array
        (
            [driver_id] => 2
            [recommendation_min] => 0
            [recommendation_max] => 50
            [recommendation_key_range] => Low
            [recommendation_observation] => Low
            [recommendation_] => Low
        )

    [1] => Array
        (
            [driver_id] => 2
            [recommendation_min] => 51
            [recommendation_max] => 78
            [recommendation_key_range] => Moderate
            [recommendation_observation] => Moderate
            [recommendation_] => Moderate
        )

)

如何合并数组,使其看起来像这样:

        [driver_id] => 2
        [recommendation_min] => 51
        [recommendation_max] => 78
        [recommendation_key_range] => Moderate
        [recommendation_observation] => Moderate
        [recommendation_] => Moderate
        [total] => 308
        [total_driver] => 5
        [score1] => 61.6000
        [score2] => 62

使用 driver_id (2) 和 Score2 (62)recommendation_key_range = 中等,因为范围 62 介于 [recommendation_min] => 51 和 [recommendation_max] => 78 之间

任何想法,谢谢

php arrays array-merge
3个回答
1
投票

基本上只需要使用

array_merge
和一些谓词语句来根据
score1
score2
来决定合并哪个数组。像这样的东西应该服务:

$drivers = array();
$drivers[0] = array('driver_id' => 2, 'total' => 308, 'total_driver' => 5, 'score1' => 61.6, 'score2' => 62 );
$drivers[1] = array('driver_id' => 3, 'total' => 354, 'total_driver' => 5, 'score1' => 70.8, 'score2' => 71 );

$recommends = array();
$recommends[0] = array('driver_id' => 2, 'recommendation_min' => 0, 'recommendation_max' => 50, 'recommendation_key_range' => "Low", 'recommendation_observation' => "Low", 'recommendation_' => "Low" );
$recommends[1] = array('driver_id' => 2, 'recommendation_min' => 51, 'recommendation_max' => 78, 'recommendation_key_range' => "Moderate", 'recommendation_observation' => "Moderate", 'recommendation_' => "Moderate" );

$results = array();
foreach($drivers as $driver) {
    if($driver['score1'] > 50 || $driver['score2'] > 50) {
        $driver = array_merge($driver,$recommends[1]);
    } else {
        $driver = array_merge($driver, $recommends[0]);
    }
    $results[] = $driver;
}

0
投票
 $drivers = array();
 $drivers[0] = array('driver_id' => 2, 'total' => 308, 'total_driver' => 5, 'score1'   => 61.6, 'score2' => 62 );
 $drivers[1] = array('driver_id' => 3, 'total' => 354, 'total_driver' => 5, 'score1' => 70.8, 'score2' => 71 );
 $drivers[2] = array('driver_id' => 4, 'total' => 354, 'total_driver' => 5, 'score1' => 70.8, 'score2' => 71 );
 $drivers[3] = array('driver_id' => 5, 'total' => 354, 'total_driver' => 5, 'score1' => 70.8, 'score2' => 71 );

 $recommends = array();
 $recommends[0] = array('driver_id' => 2, 'recommendation_min' => 0, 'recommendation_max' => 50, 'recommendation_key_range' => "Low", 'recommendation_observation' => "Low", 'recommendation_' => "Low" );

 $recommends[1] = array('driver_id' => 2, 'recommendation_min' => 51, 'recommendation_max' => 78, 'recommendation_key_range' => "Moderate", 'recommendation_observation' => "Moderate", 'recommendation_' => "Moderate" );
 $recommends[2] = array('driver_id' => 4, 'recommendation_min' => 51, 'recommendation_max' => 78, 'recommendation_key_range' => "Moderate", 'recommendation_observation' => "Moderate", 'recommendation_' => "Moderate" );


 $one = array();
 $matched = array();
 $flag = 0;
 $myarray = array();

 foreach($drivers as $key=>$data){   
    foreach($recommends as $keys=>$dt){
       if($data['driver_id']==$dt['driver_id']){            
          $one = array_merge($data , $dt);
       }
     }

    if(!empty($matched)){
        foreach($matched as $key=>$dta){

           if($dta['driver_id']==$one['driver_id']) 
             $flag = 1;
         }

         if($flag==0){
            $matched[] = $one;
            $myarray[] = $one['driver_id'];
         }

      $flag=0;

     }else{
       $matched[] = $one;
       $myarray[] = $one['driver_id'];
     }
 }
$myarray = array_unique($myarray);

foreach($drivers as $key=>$data){   
    if(!in_array($data['driver_id'] , $myarray)){
       $matched[] = $data;
     }
}

foreach($recommends as $key=>$dta){   
    if(!in_array($dta['driver_id'] , $myarray)){
        $matched[] = $data;
    }
}

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

0
投票

从 PHP8.4 开始,您可以使用

array_find()
享受短路循环和函数式编程的性能。 它将搜索限定值(在本例中为一行),并返回第一个限定符,如果未找到匹配则返回
null

暂时将每个对象转换为数组,以便与找到的数组合并,然后将更新的有效负载恢复为对象。 演示

var_export(
    array_map(
        fn($obj) => (object)array_merge(
            array_find(
                $array2,
                fn($row) => $obj->driver_id == $row['driver_id']
                    && $obj->score2 >= $row['recommendation_min']
                    && $obj->score2 <= $row['recommendation_max']
            ) ?? [],
            (array)$obj
        ),
        $array1
    )
);

输出:

array (
  0 => 
  (object) array(
     'driver_id' => 2,
     'recommendation_min' => 51,
     'recommendation_max' => 78,
     'recommendation_key_range' => 'Moderate',
     'recommendation_observation' => 'Moderate',
     'recommendation_' => 'Moderate',
     'total' => 308,
     'total_driver' => 5,
     'score1' => 61.6,
     'score2' => 62,
  ),
  1 => 
  (object) array(
     'driver_id' => 3,
     'total' => 354,
     'total_driver' => 5,
     'score1' => 70.8,
     'score2' => 71,
  ),
)
© www.soinside.com 2019 - 2024. All rights reserved.