Viewed   145 times

I have an array with the following format:

Array
(
    [0] => Array
        (
            [DateTime] => "2013-05-22 14:21:01"
            [Price] => 102.01
        )
    [1] => Array
        (
            [DateTime] => "2013-05-23 15:55:01"
            [Price] => 52.60
        )
    [2] => Array
        (
            [DateTime] => "2013-05-25 14:23:01"
            [Price] => 452.25
        )
    ... etc
)

I need to discover the lowest and highest value of Price.

min only returns they key. I've also tried max(array_map("max", $data)) but that only returns 452.25.

Will I have to use a foreach and do it manually?

 Answers

1

Here's one way to get the min and max values:

$min = min(array_column($array, 'Price'));
$max = max(array_column($array, 'Price'));

To return the nested array for the min and max:

$prices = array_column($array, 'Price');
$min_array = $array[array_search(min($prices), $prices)];
$max_array = $array[array_search(max($prices), $prices)];

You could do each in one line since that looked like what you were trying to do:

$min_array = $array[array_search(min($prices = array_column($array, 'Price')), $prices)];
$max_array = $array[array_search(max($prices = array_column($array, 'Price')), $prices)];

PHP >= 5.5.0 needed for array_column() or use the PHP Implementation of array_column().

Using array_map() to get just the min and max:

$min = min(array_map(function($a) { return $a['Price']; }, $array));
$max = max(array_map(function($a) { return $a['Price']; }, $array));

There's probably a good array_filter() or array_reduce() as well.

Thursday, September 22, 2022
 
reloecc
 
4
$max = 0;
foreach($array as $obj)
{
    if($obj->dnum > $max)
    {
        $max = $obj->dnum;
    }
}

That function would work correctly if your highest number is not negative (negatives, empty arrays, and 0s will return the max as 0).

Because you are using an object, which can have custom properties/structures, I don't believe there are really any 'predefined' functions you can use to get it. Might as well just use a foreach loop.

You really can't get away from a foreach loop, as even internal functions use a foreach loop, it is just behind the scenes.

Another solution is

$numbers = array();
foreach($array as $obj)
{
    $numbers[] = $obj->dnum;
}
$max = max($numbers);
Saturday, October 8, 2022
1

You are using the equality operator == rather than the assignment operator = in your else statement.

Sunday, October 2, 2022
 
4

how about the following:

// assume students.length > 0
static void printMinMax(Student[] students) {
  Student min = students[0];
  Student max = students[0];

  for (Student student: students) {
    if (student.getGrade() > max.getGrade())
      max = student;
    if (student.getGrade() < min.getGrade())
      min = student;
  }

  System.out.println("Best student: " + max);
  System.out.println("Worst student: "+ min);
}

On another note, you should really consider using Collections instead of plain arrays. Especially since you do not really know the number of students beforehand.

Above code could be rewritten like this:

static void printMinMax(List<Student> students) {
  Comparator<Student> comparator = new Comparator<>() {
    @Override public int compare(Student s1, Student s2) {
      return s1.getGrade() - s2.getGrade();
    }
  };
  Student max = Collections.max(students, comparator);
  Student min = Collections.min(students, comparator);

  // print stuff
}

or shorter using java 8 lambdas:

static void printMinMax(List<Student> students) {
  Comparator<Student> comparator = (s1, s2) -> s1.getGrade() - s2.getGrade();

  Student max = Collections.max(students, comparator);
  Student min = Collections.min(students, comparator);

  // print stuff
}
Sunday, September 11, 2022
 
zeiger
 
3

Something like

type
  TSmallGrid = array[1..3, 1..3] of Integer;
  TBigGrid = array[1..3, 1..3] of TSmallGrid;

should work. Access to BigArray: TBigGrid would be with standard Pascal array syntax:

  MyInt := BigArray[1, 2, 1, 2]; // or even BigArray[1, 2][1, 2] to emphasize the nesting

or

  SmallArray := BigArray[1, 2];
Monday, August 15, 2022
 
gcb
 
gcb
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :