Skip to content

Instantly share code, notes, and snippets.

@zgoniaiko
Last active October 5, 2018 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zgoniaiko/017f18cf85d6011c0c762da8a889244b to your computer and use it in GitHub Desktop.
Save zgoniaiko/017f18cf85d6011c0c762da8a889244b to your computer and use it in GitHub Desktop.
Function to find the maximum value in a nested set of arrays
<?php
namespace App\Entity;
class MyMax
{
public function find(array $input) {
$max = null;
foreach ($input as $value) {
$result = null;
if (is_array($value)) {
$result = $this->find($value);
}
if (is_numeric($value)) {
$result = $value;
}
$max = ($result > $max) ? $result : $max;
}
return $max;
}
}
<?php
namespace App\Tests;
use App\Entity\MyMax;
use PHPUnit\Framework\TestCase;
class MyMaxTest extends TestCase
{
public function testPlainArray()
{
$finder = new MyMax();
$this->assertNull($finder->find([]));
$this->assertEquals(1, $finder->find([1]));
$this->assertEquals(2, $finder->find([1, 2]));
$this->assertEquals(3, $finder->find([1, 1 + 2]));
}
public function testNestedArray()
{
$finder = new MyMax();
$this->assertNull($finder->find([[]]));
$this->assertEquals(2, $finder->find([1, [1, 2]]));
$this->assertEquals(3, $finder->find([3, [1, 2]]));
$this->assertEquals(3, $finder->find([3, [1, 2], [2, 3]]));
$this->assertEquals(4, $finder->find([3, [1, 2], [4, 3]]));
$this->assertEquals(5, $finder->find([3, [1, 2, [4, 5]], [4, 3]]));
$this->assertEquals(1000, $finder->find([1, 2, [3, [1000, 4, 5], 6, 7], 8, 9]));
$this->assertEquals(1000, $finder->find([1, 2, [3, [1000, 4, 5, '999'], 6, 7], 8, 9]));
$this->assertEquals(1000, $finder->find([1, 2, [3, ['1000', 4, 5], 6, 7], 8, 9]));
$this->assertEquals(1000, $finder->find([1, 2, [3, ['key' => 1000, 4, 5], 6, 7], 8, 9]));
$this->assertEquals(1000, $finder->find([1, 2, [3, ['key' => '1000', 4, 5], 6, 7], 8, 9]));
$this->assertEquals(1000, $finder->find([1, 2, [3, [1000, 4, 5, new \stdClass()], 6, 7], 8, 9]));
}
public function testFloat()
{
$finder = new MyMax();
$this->assertEquals(1, $finder->find([1, 0.5]));
$this->assertEquals(1, $finder->find([1, 0.5]));
}
public function testInfinity()
{
$finder = new MyMax();
$this->assertEquals(INF, $finder->find([1, INF]));
$this->assertEquals(1, $finder->find([1, -INF]));
$this->assertEquals(1, $finder->find([1, log(0)]));
}
public function testNan()
{
$finder = new MyMax();
$this->assertEquals(1, $finder->find([1, NAN]));
$this->assertEquals(-1, $finder->find([-1, NAN]));
}
public function testNull()
{
$finder = new MyMax();
$this->assertNull($finder->find([null]));
$this->assertEquals(1, $finder->find([1, null]));
$this->assertEquals(-1, $finder->find([-1, null]));
}
public function testNegative()
{
$finder = new MyMax();
$this->assertEquals(-1, $finder->find([-1, -2]));
$this->assertEquals(-1, $finder->find(['-1', -2]));
}
public function testString()
{
$finder = new MyMax();
$this->assertEquals(1, $finder->find([1, 'a']));
$this->assertEquals(1, $finder->find([1, '22a']));
$this->assertEquals(1, $finder->find([1, 'a22']));
$this->assertEquals(1000, $finder->find([1, 2, [3, [1000, 4, 5, 'test'], 6, 7], 8, 9]));
}
}
<?php
function my_max(array $xs) {
$max = null;
foreach ($xs as $value) {
$result = null;
if (is_array($value)) {
$result = my_max($value);
}
if (is_numeric($value)) {
$result = $value;
}
$max = ($result > $max) ? $result : $max;
}
return $max;
}
echo my_max([1, 2, [3, [1000, 4, 5], 6, 7], 8, 9]) . "\n"; // 1000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment