@@ -392,6 +392,11 @@ private function process($original, $new)
392
392
return is_array ($ new ) ? $ newOrdered : (object )$ newOrdered ;
393
393
}
394
394
395
+ /**
396
+ * @param array $original
397
+ * @param array $new
398
+ * @return array
399
+ */
395
400
private function rearrangeArray (array $ original , array $ new )
396
401
{
397
402
$ first = reset ($ original );
@@ -412,7 +417,7 @@ private function rearrangeArray(array $original, array $new)
412
417
413
418
$ keyIsUnique = true ;
414
419
$ uniqueIdx = array ();
415
- foreach ($ original as $ item ) {
420
+ foreach ($ original as $ idx => $ item ) {
416
421
if (!$ item instanceof \stdClass) {
417
422
return $ new ;
418
423
}
@@ -430,7 +435,7 @@ private function rearrangeArray(array $original, array $new)
430
435
$ keyIsUnique = false ;
431
436
break ;
432
437
}
433
- $ uniqueIdx [$ value ] = true ;
438
+ $ uniqueIdx [$ value ] = $ idx ;
434
439
}
435
440
436
441
if ($ keyIsUnique ) {
@@ -439,43 +444,54 @@ private function rearrangeArray(array $original, array $new)
439
444
}
440
445
}
441
446
442
- if ($ uniqueKey ) {
443
- $ newIdx = array ();
444
- foreach ($ new as $ item ) {
445
- if (!$ item instanceof \stdClass) {
446
- return $ new ;
447
- }
447
+ if (!$ uniqueKey ) {
448
+ return $ new ;
449
+ }
448
450
449
- if (!property_exists ($ item , $ uniqueKey )) {
450
- return $ new ;
451
- }
451
+ $ newRearranged = [];
452
+ $ changedItems = [];
452
453
453
- $ value = $ item ->$ uniqueKey ;
454
+ foreach ($ new as $ item ) {
455
+ if (!$ item instanceof \stdClass) {
456
+ return $ new ;
457
+ }
454
458
455
- if ($ value instanceof \stdClass || is_array ( $ value )) {
456
- return $ new ;
457
- }
459
+ if (! property_exists ( $ item , $ uniqueKey )) {
460
+ return $ new ;
461
+ }
458
462
459
- if (isset ($ newIdx [$ value ])) {
460
- return $ new ;
461
- }
463
+ $ value = $ item ->$ uniqueKey ;
462
464
463
- $ newIdx [$ value ] = $ item ;
465
+ if ($ value instanceof \stdClass || is_array ($ value )) {
466
+ return $ new ;
464
467
}
465
468
466
- $ newRearranged = array ();
467
- foreach ($ uniqueIdx as $ key => $ item ) {
468
- if (isset ($ newIdx [$ key ])) {
469
- $ newRearranged [] = $ newIdx [$ key ];
470
- unset($ newIdx [$ key ]);
469
+
470
+ if (isset ($ uniqueIdx [$ value ])) {
471
+ $ idx = $ uniqueIdx [$ value ];
472
+ // Abandon rearrangement if key is not unique in new array.
473
+ if (isset ($ newRearranged [$ idx ])) {
474
+ return $ new ;
471
475
}
476
+
477
+ $ newRearranged [$ idx ] = $ item ;
478
+ } else {
479
+ $ changedItems [] = $ item ;
472
480
}
473
- foreach ($ newIdx as $ item ) {
474
- $ newRearranged [] = $ item ;
481
+
482
+ $ newIdx [$ value ] = $ item ;
483
+ }
484
+
485
+ $ idx = 0 ;
486
+ foreach ($ changedItems as $ item ) {
487
+ while (array_key_exists ($ idx , $ newRearranged )) {
488
+ $ idx ++;
475
489
}
476
- return $ newRearranged ;
490
+ $ newRearranged[ $ idx ] = $ item ;
477
491
}
478
492
479
- return $ new ;
493
+ ksort ($ newRearranged );
494
+ $ newRearranged = array_values ($ newRearranged );
495
+ return $ newRearranged ;
480
496
}
481
497
}
0 commit comments