Advertisement
Guest User

Untitled

a guest
Jan 1st, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.56 KB | None | 0 0
  1. <?php
  2.  
  3. $arr = array(
  4.     array('id' => 15, 'parent' => 12),
  5.     array('id' => 10, 'parent' => 12),
  6.     array('id' => 12, 'parent' => 12),
  7.     array('id' => 17, 'parent' => 12),
  8.     array('id' => 21, 'parent' => 15),
  9.     array('id' => 13, 'parent' => 15),
  10.     array('id' => 15, 'parent' => 15),
  11.     array('id' => 25, 'parent' => 15),
  12.     array('id' => 7, 'parent' => 7),
  13.     array('id' => 18, 'parent' => 7),
  14.     array('id' => 4, 'parent' => 7),
  15.     array('id' => 1, 'parent' => 3),
  16.     array('id' => 5, 'parent' => 5),
  17.     array('id' => 2, 'parent' => 7)
  18. );
  19.  
  20. /* Declare variables */
  21. $parents = array();
  22. $result = array();
  23.  
  24. /* Get all distinct parents */
  25. for ($x = 0; $x < count($arr); $x++)
  26.     if ((!in_array($arr[$x]['parent'], $parents)))
  27.         $parents[] = $arr[$x]['parent'];
  28.  
  29. sort($parents);
  30.  
  31. /* Iterate through each parent and create result array */
  32. for ($i = 0; $i < count($parents); $i++) {
  33.  
  34.     /* Find parent with matching id */
  35.     for ($x = 0; $x < count($arr); $x++) {
  36.         if (($arr[$x]['parent'] === $parents[$i]) && ($arr[$x]['id'] === $parents[$i])) {
  37.             $result[] = $arr[$x];
  38.             $arr[$x] = NULL;
  39.             break;
  40.         }
  41.     }
  42.  
  43.     /* No match found, find parent with lowest id */
  44.     if ((count($result) === 0) || ($result[count($result)-1]['parent'] !== $parents[$i])) {
  45.         $lowest = array(
  46.             'key' => -1,
  47.             'value' => -1
  48.         );
  49.  
  50.         for ($x = 0; $x < count($arr); $x++) {
  51.             if ($arr[$x]['parent'] !== $parents[$i] || (in_array($arr[$x], $result)))
  52.                 continue;
  53.  
  54.             if ($lowest['value'] === -1 || $arr[$x]['id'] < $lowest['value']) {
  55.                 $lowest['key'] = $x;
  56.                 $lowest['value'] = $arr[$x]['id'];
  57.             }
  58.         }
  59.  
  60.         $result[] = $arr[$lowest['key']];
  61.         $arr[$lowest['key']] = NULL;
  62.     }
  63.  
  64.     /* Add the rest of the elements for current parent */
  65.     for ($x = 0; $x < count($arr); $x++) {
  66.         if (($arr[$x] === NULL) || ($arr[$x]['parent'] !== $parents[$i]) || (in_array($arr[$x], $result)))
  67.             continue;
  68.  
  69.         $lowest = array(
  70.             'key' => -1,
  71.             'value' => -1
  72.         );
  73.  
  74.         for ($y = 0; $y < count($arr); $y++) {
  75.             if (($arr[$y] === NULL) || $arr[$y]['parent'] !== $parents[$i] || (in_array($arr[$y], $result)))
  76.                 continue;
  77.  
  78.             if (($lowest['value'] === -1) || ($arr[$y]['id'] < $lowest['value'])) {
  79.                 $lowest['key'] = $y;
  80.                 $lowest['value'] = $arr[$y]['id'];
  81.             }
  82.  
  83.         }
  84.  
  85.         $result[] = $arr[$lowest['key']];
  86.         $arr[$lowest['key']] = NULL;
  87.     }
  88.  
  89.     /* Add the last element for current parent (didn't find any other solution) */
  90.     for ($x = 0; $x < count($arr); $x++)
  91.         if ($arr[$x] !== NULL && $arr[$x]['parent'] === $parents[$i])
  92.             $result[] = $arr[$x];
  93. }
  94.  
  95. /* Output */
  96. print_r($result);
  97.  
  98. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement