Searching for duplicates in multidimensional array PHP -
i have multidimensional php array looking in json:
array = [{ "id":"1", "key":"ferrari", "type":"car"}, { "id":"1", "key":"red", "type":"color"}, { "id":"73", "key":"yellow", "type":"color" }]
because it's search result, to, dynamically combine results id's same. in other words new array should like:
array = [{ "id":"1", "key":"red ferrari", "type":"keyword"}, { "id":"73", "key":"yellow", "type":"color" }]
i have been looking @ lot of php functions, have limited functionality, in multidimensional arrays. suggestions appreciated :) many regards andreas
there's no base of operations functions specific thing mention. can create this:
print_r(my_array_merge($data)); function my_array_merge($data) { // sort records id merge them later $data_by_id = array(); foreach ($data $item) { if (!isset($data_by_id[ $item['id'] ])) { $data_by_id[ $item['id'] ] = array(); } $data_by_id[ $item['id'] ][] = $item; } // merge records same id $return = array(); foreach ($data_by_id $id => $items) { foreach ($items $item) { if (!isset($return[ $id ])) { $return[ $id ] = $item; } else { $return[ $id ]['key'] .= ' ' . $item['key']; $return[ $id ]['type'] = 'keyword'; // don't mean 'keyword' } } } // reset keys before homecoming homecoming array_values($return); }
php arrays multidimensional-array
No comments:
Post a Comment