Skip to content

Commit 4f44111

Browse files
committed
Fixing empty arrays being dropped with Set::sort(). Fixed #67
1 parent b5a38d6 commit 4f44111

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

cake/libs/set.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,7 @@ function __flatten($results, $key = null) {
10731073
if (!is_null($key)) {
10741074
$id = $key;
10751075
}
1076-
if (is_array($r)) {
1076+
if (is_array($r) && count($r)) {
10771077
$stack = array_merge($stack, Set::__flatten($r, $id));
10781078
} else {
10791079
$stack[] = array('id' => $id, 'value' => $r);

cake/tests/cases/libs/set.test.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,21 @@ function testSort() {
336336
);
337337
$a = Set::sort($a, '{n}.Person.name', 'ASC');
338338
$this->assertIdentical($a, $b);
339+
340+
$names = array(
341+
array('employees' => array(array('name' => array('first' => 'John', 'last' => 'Doe')))),
342+
array('employees' => array(array('name' => array('first' => 'Jane', 'last' => 'Doe')))),
343+
array('employees' => array(array('name' => array()))),
344+
array('employees' => array(array('name' => array())))
345+
);
346+
$result = Set::sort($names, '{n}.employees.0.name', 'asc', 1);
347+
$expected = array(
348+
array('employees' => array(array('name' => array('first' => 'John', 'last' => 'Doe')))),
349+
array('employees' => array(array('name' => array('first' => 'Jane', 'last' => 'Doe')))),
350+
array('employees' => array(array('name' => array()))),
351+
array('employees' => array(array('name' => array())))
352+
);
353+
$this->assertEqual($result, $expected);
339354
}
340355
/**
341356
* testExtract method

0 commit comments

Comments
 (0)