douge3830 2018-01-17 16:43
浏览 46
已采纳

基于数组值的实例匹配多维数组,并仅返回1个结果

I need to be able to match up arrays, based on instances of row values and only return 1 result.

I'm not sure if i'm phrasing that right, so let me explain.

For example, lets say I have 2 arrays

  $people = array(
    array(
        'name' => 'Joe',
        'DOB' => '01/01/1990'
    ),
    array(
        'name' => 'John',
        'DOB' => '01/01/1990'
    ),
    array(
        'name' => 'Jane',
        'DOB' => '01/01/1990'
    )
  );
  $sales = array(
    array(
        'name' => 'Car',
        'price' => '1000.00',
        'note' => 'This sale was made by Joe Bloggs'
    ),
    array(
        'name' => 'Car 2',
        'price' => '2000.00',
        'note' => 'Joe Bloggs sold this car today'
    ),
    array(
        'name' => 'Car 3',
        'price' => '3000.00'
        'note' => 'Jane Smith sold our most expensive car'
    )
  );


 foreach($people as $personKey => $person){
      foreach($sales as $saleKey => $sale){
          if($sale['name'] == $person['name']){
              if($sale['price'] > 1000.00){
                  $person[$personKey]['highestSale'] == $saleKey;
              } else {
                  $person[$personKey]['highestSale'] == $saleKey;
              }
          }
      }
 }

I want to be able to run the foreach loop, but achieve 2 things with it

  1. I need to be able to search the second foreach loop, by the current first name only. I need it to be able to search the $sales['seller'] string and match it if it contains the $people['name'] (Joe will match with Joe Bloggs).

  2. I also need it to take priority of the first result only in this loop. So the if($sale['price'] > 1000.00) is true, exit this instance of the person loop, and move on to the next person (only return 1 result/match for each person);

At the moment it would return

"Joe's highest sale is £2000.00"

"Joe's highest sale is £1000.00"

"Jane's highest sale is £3000.00"

"John's highest sale is £0.00"

Inside the loop, where the if statement runs, I need it to exit the current loop if the if statement is true and move onto the next row, so my result will look like this instead.

"Joe's highest sale is £2000.00"

"Jane's highest sale is £3000.00"

"John's highest sale is £0.00"

I'm sorry if this doesn't make sense, its quite hard to explain what it is i'm trying to achieve! If anything needs to be re-explained, please ask.

  • 写回答

2条回答 默认 最新

  • douzhuoxia0587 2018-01-17 17:00
    关注
      // Collect names
      $person = array_fill_keys(array_column($people, 'name'), [0]);
      // Add all sales to each seller     
      foreach($sales as $saleKey => $sale){
         if($sale['price'] > 1000.00) {
            $person[explode(' ', $sale['seller'])[0]][] = $sale['price'];
         }
      }
      // Print  names and max value
      foreach($person as $k=>$v) { 
        echo $k ."'s highest sale is £" .  max($v) ."<br>
    ";
      }
    

    demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?