<?php $arr = [ [ 'TYPE' => 'Answer', 'PARTY_ID' => 115, 'PARENT_USER_CONTENT_ID' => 114 ], [ 'TYPE' => 'Question', 'PARTY_ID' => 112, 'PARENT_USER_CONTENT_ID' => '' ], [ 'TYPE' => 'Question', 'PARTY_ID' => 113, 'PARENT_USER_CONTENT_ID' => '' ], [ 'TYPE' => 'Answer', 'PARTY_ID' => 116, 'PARENT_USER_CONTENT_ID' => 113 ], [ 'TYPE' => 'Question', 'PARTY_ID' => 114, 'PARENT_USER_CONTENT_ID' => '' ], [ 'TYPE' => 'Answer', 'PARTY_ID' => 117, 'PARENT_USER_CONTENT_ID' => 112 ] ]; $indexed = array_combine(array_column($arr, 'PARTY_ID'), $arr); $answers = array_flip(array_filter(array_column($arr, 'PARENT_USER_CONTENT_ID', 'PARTY_ID'))); foreach ($answers as $parentPartyId => $childPartyId) { $result[] = array_merge($indexed[$parentPartyId], [ 'ANSWER' => $indexed[$childPartyId]]); } print_r($result);
You have javascript disabled. You will not be able to edit any code.