fork download
  1. <?php
  2.  
  3. $array = array(
  4. 5 => array (
  5. 'id' => 5,
  6. 'content' => 'some text',
  7. 'id_rel' => 88
  8. ),
  9. 49 => array (
  10. 'id' => 49,
  11. 'content'=> 'some text',
  12. 'id_rel' => NULL
  13. ),
  14. 88 => array (
  15. 'id' => 88,
  16. 'content' => 'some text',
  17. 'id_rel' => 5
  18. ),
  19. 3 => array (
  20. 'id' => 3,
  21. 'content' => 'some text',
  22. 'id_rel' => NULL
  23. )
  24. );
  25.  
  26. $newArray = array();
  27. ksort($array);
  28. foreach ($array as $key => $val) {
  29. // Add it to the new array
  30. $newArray[$key] = $val;
  31. // Remove it
  32. unset($array[$key]);
  33.  
  34. // If no id_rel, move on
  35. if (empty($val['id_rel'])) {
  36. continue;
  37. }
  38.  
  39. // Get id_rel's
  40. foreach ($array as $subKey => $subVal) {
  41. if ($subKey == $val['id_rel']) {
  42. $newArray[$subKey] = $subVal;
  43. unset($array[$subKey]);
  44. }
  45. }
  46. }
  47. unset($array, $subKey, $subVal, $key, $val);
  48. print_r($newArray);
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
Array
(
    [3] => Array
        (
            [id] => 3
            [content] => some text
            [id_rel] => 
        )

    [5] => Array
        (
            [id] => 5
            [content] => some text
            [id_rel] => 88
        )

    [88] => Array
        (
            [id] => 88
            [content] => some text
            [id_rel] => 5
        )

    [49] => Array
        (
            [id] => 49
            [content] => some text
            [id_rel] => 
        )

)