@ 2013-11-17T00:01:10Z <?php
abstract class AHeap extends SplHeap
{
public function __construct($i)
{
foreach ($i as $obj) {
$this->insert($obj);
}
}
}
class KeySort extends AHeap
{
public function compare($val1, $val2)
{
if ($val1->key == $val2->key)
return 0;
if ($val1->key > $val2->key){
return -1;
}
else {
return 1;
}
}
}
class ValueSort extends AHeap
{
public function compare($val1, $val2)
{
if ($val1->value == $val2->value)
return 0;
if ($val1->value < $val2->value){
return -1;
}
else {
return 1;
}
}
}
$array = array(
(object) array('key' => 0, 'value' => 'z'),
(object) array('key' => 0, 'value' => 'a'),
(object) array('key' => 2, 'value' => 'y'),
(object) array('key' => 3, 'value' => 'b'),
);
$heap = new KeySort($array);
$heap2 = new ValueSort($heap);
foreach ($heap2 as $val) {
echo $val->key . ' - ' . $val->value . PHP_EOL;
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.6 Deprecated: Return type of KeySort::compare($val1, $val2) should either be compatible with SplHeap::compare(mixed $value1, mixed $value2): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/3AqnE on line 15
Deprecated: Return type of ValueSort::compare($val1, $val2) should either be compatible with SplHeap::compare(mixed $value1, mixed $value2): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/3AqnE on line 31
0 - z
2 - y
3 - b
0 - a
Output for 5.3.0 - 5.3.29 , 5.4.0 - 5.4.45 , 5.5.0 - 5.5.38 , 5.6.0 - 5.6.40 , 7.0.0 - 7.0.33 , 7.1.0 - 7.1.33 , 7.2.0 - 7.2.33 , 7.3.0 - 7.3.33 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.30 0 - z
2 - y
3 - b
0 - a
Output for 5.0.0 - 5.0.5 , 5.1.0 - 5.1.6 , 5.2.0 - 5.2.17 Fatal error: Class 'SplHeap' not found in /in/3AqnE on line 3
Process exited with code 255 . Output for 4.4.2 - 4.4.9 Parse error: syntax error, unexpected T_CLASS in /in/3AqnE on line 3
Process exited with code 255 . Output for 4.3.0 - 4.3.1 , 4.3.5 - 4.3.11 , 4.4.0 - 4.4.1 Parse error: parse error, unexpected T_CLASS in /in/3AqnE on line 3
Process exited with code 255 . Output for 4.3.2 - 4.3.4 Parse error: parse error in /in/3AqnE on line 3
Process exited with code 255 . preferences:dark mode live preview
303.67 ms | 401 KiB | 460 Q