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 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号