将MultiDimensional数组中的数组/元素作为子数组移动,具体取决于某列的值

Array
(
  [0] => Array
    (
      [id] => 50
      [append_parent] => 51
      [data] => Array
        (
          [0] => Array
            (
              [id] => 43
              [append_parent] => 
            )

          [1] => Array
            (
              [id] => 44
              [append_parent] => 
            )
        )
    )

  [1] => Array
    (
      [id] => 51
      [append_parent] => 
      [data] => Array
        (
          [0] => Array
            (
              [id] => 41
              [append_parent] => 
            )

          [1] => Array
            (
              [id] => 42
              [append_parent] => 43
            )
        )
    )
)

This is an example array, I am trying to work within this MultiDimentional array in order to move (not copy but move) arrays/ elements as a child arrays/element if that array has a value mentioned in the append_parent column that matches to an array/ elements id column. For e.g if an array has append_parent = 42, it should become a child array/ element to the array/ element that has a column with id = 42. The same way all array/ elements with values in the append_parent column should become child arrays/ elements to arrays that match the value on the id columns.

I am trying the below approach, but not reaching anywhere

function SearchAndAppend($array, $col, $value) {
  foreach($array as $key => $val) {
    $temp_array = array();
    $temp_array = $array;
    if (!is_array($val)) {
      if ($key == $col && $val == $value){
        $temp_array[] = $element_to_append;
      }
      } else {
      if ($this->SearchAndAppend($val, $col, 42))
      $temp_array[] = $element_to_append;
    }
  }
  return $temp_array;
}

$sorted_array = $this->SearchAndAppend($arr, "id", "append_parent");

The array if needed

$a1[0] = array("id"=>"34", "append_parent"=>"42");
$a1[1] = array("id"=>"35", "append_parent"=>"41");

$a1[0] = array("id"=>"36", "append_parent"=>"");
$a1[1] = array("id"=>"37", "append_parent"=>"44");

$x1[0] = array("id"=>"41", "append_parent"=>"");
$x1[1] = array("id"=>"42", "append_parent"=>"43");

$x2[0] = array("id"=>"43", "append_parent"=>"");
$x2[1] = array("id"=>"44", "append_parent"=>"");

$arr[0] = array("id"=>"50", "append_parent"=>"51", "data"=>$x2);
$arr[1] = array("id"=>"51", "append_parent"=>"", "data"=>$x1);