Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have this kind of array:

array(2) {
[1] => array(3) {
  [3] => array(3) {
    ["data"] => array(3) {
      ["id"] => string(1) "3"
      ["depth"] => string(1) "1"
      ["parent_id"] => NULL
    }
  }
  [4] => array(3) {
    ["data"] => array(3) {
      ["id"] => string(1) "4"
      ["depth"] => string(1) "1"
      ["parent_id"] => NULL
    }
  }
  [2] => array(3) {
    ["data"] => array(3) {
      ["id"] => string(1) "2"
      ["depth"] => string(1) "1"
      ["parent_id"] => NULL
    }
  }
}
[2] => &array(3) {
  [15] => array(3) {
    ["data"] => array(3) {
      ["id"] => string(2) "15"
      ["depth"] => string(1) "2"
      ["parent_id"] => string(1) "3"
    }
  }
  [16] => array(3) {
    ["data"] => array(3) {
      ["id"] => string(2) "16"
      ["depth"] => string(1) "2"
      ["parent_id"] => string(1) "2"
    }
  }
  [18] => array(3) {
    ["data"] => array(3) {
      ["id"] => string(2) "18"
      ["depth"] => string(1) "2"
      ["parent_id"] => string(1) "4"
    }
  }
}
}

First level means depth (1 hasn't child, 2 has parent without child, etc.). Order of array items is correct. I need obtain different array in the same order (of course in dependency of level).

array(6) {
[3] => array(3) {
  ["data"] => array(3) {
    ["id"] => string(1) "3"
    ["depth"] => string(1) "1"
    ["parent_id"] => NULL
  }
}
[15] => array(3) {
  ["data"] => array(3) {
    ["id"] => string(1) "15"
    ["depth"] => string(1) "1"
    ["parent_id"] => 3
  }
}
[4] => array(3) {
  ["data"] => array(3) {
    ["id"] => string(1) "4"
    ["depth"] => string(1) "1"
    ["parent_id"] => NULL
  }
}
[18] => &array(3) {
  ["data"] => array(3) {
    ["id"] => string(2) "18"
    ["depth"] => string(1) "2"
    ["parent_id"] => string(1) "4"
  }
}
[2] => array(3) {
  ["data"] => array(3) {
    ["id"] => string(2) "2"
    ["depth"] => string(1) "2"
    ["parent_id"] => NULL
  }
}
[16] => array(3) {
  ["data"] => array(3) {
    ["id"] => string(2) "16"
    ["depth"] => string(1) "2"
    ["parent_id"] => string(1) "2"
  }
}
}
share|improve this question
1  
and you have tried, what ? –  njzk2 Mar 7 '13 at 10:56
    
I tried everything, but I can't get it. I think that will be need use some recursion, but I don't know how. Could you advice please? –  tomasr Mar 7 '13 at 10:59
    
I made first array from the same like second one. First array is just because of multidimensional sorting (alphabethical, but by arbitrary parameter (these are missing for simplicity)). But return to origin array only in different order is much more complicated. –  tomasr Mar 7 '13 at 11:05
    
you want the combination of array[1] and array[2] ? tried array_merge ? –  njzk2 Mar 7 '13 at 11:45
    
'+' might work just as well, too –  njzk2 Mar 7 '13 at 11:45

1 Answer 1

up vote 0 down vote accepted

**

First solution

**

I found solution but I do not know how make recursion from that. It works only with two levels. Third level is order badly. Variable $this->hierarchicalData contains first array.

$_sortedHierarchyData = array();
foreach ($this->hierarchicalData as $levelKey => $_level) {
  foreach ($_level as $itemKey => $item) {
    if (isset($this->hierarchicalData[$levelKey + 1])) {
      $_sortedHierarchyData[$item['data']['id']] = $item;
      $children = $this->searchChildrenFromLevelOfHieararchicalSortedData($item['data']['id'], $this->hierarchicalData[$levelKey + 1]);
      $_sortedHierarchyData += $children;
    }               
  }
}

function searchChildrenFromLevelOfHieararchicalSortedData($parent_id, $level)
{
  $_children = array();
  foreach ($level as $key => $item) {
    if ($item['data']['parent_id'] === $parent_id) {
      $_children[$key] = $item;
    }
  }
  return $_children;
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.