user7070541
user7070541

Reputation:

Match multidimensional arrays based on instances of array values and return only 1 result

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.

Upvotes: 0

Views: 57

Answers (2)

splash58
splash58

Reputation: 26143

  // 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>\n";
  }

demo

Upvotes: 0

Don&#39;t Panic
Don&#39;t Panic

Reputation: 41810

First sort the sales in descending order by price.

usort($sales, function($a, $b) {
    return $b['price'] <=> $a['price'];
});

Then when you iterate the people, you can print the first match from the sales array, and that will be their highest sale. If there are no matches, then their highest sale was 0.

foreach ($people as $person) {
    $highest = '0.00';                                             //default to 0
    foreach ($sales as $saleKey => $sale) {
        if (strpos($sale['note'], $person['name']) !== false) {
            $highest = $sale['price'];
            break;                                                //stop after 1st match
        }
    }
    echo "$person[name]'s highest sale is £$highest<br>";
}

I'm honestly not sure how the >1000 factors into it.

Upvotes: 1

Related Questions