fork download
  1. <?php
  2.  
  3. $arrayTable = array(
  4. 0 => array(
  5. 'idApiUm' => 123,
  6. 'title' => 'Teste'
  7. ),
  8. 1 => array (
  9. 'idApiDois' => 765,
  10. 'title' => 'Título'
  11. ),
  12. 2 => array(
  13. 'idApiUm' => 632,
  14. 'title' => 'Nome'
  15. ),
  16. 3 => array(
  17. 'idApiDois' => 999,
  18. 'title' => 'Teste'
  19. ),
  20. );
  21.  
  22. function get_items($array, $field)
  23. {
  24. return array_unique(array_map(function($value) use ($field) {
  25. return $value[$field];
  26. }, $array)
  27. );
  28. }
  29.  
  30. function get_index($array, $field, $search) {
  31. return array_keys(
  32. array_filter($array,
  33. function ($value) use ($search, $field) {
  34. return (strpos($value[$field], $search) !== false);
  35. }
  36. )
  37. );
  38. }
  39.  
  40.  
  41. $array_new = array();
  42. foreach (get_items($arrayTable, 'title') as $value)
  43. {
  44. $array_index = get_index($arrayTable, 'title', $value);
  45. $array_index_item = array();
  46. foreach ($array_index as $i)
  47. {
  48. if (count($array_index_item) == 0)
  49. {
  50. $array_index_item = $arrayTable[$i];
  51. }
  52. else
  53. {
  54. $array_index_item = array_merge($array_index_item, $arrayTable[$i]);
  55. }
  56. }
  57. $array_new[] = $array_index_item;
  58. }
  59.  
  60. var_dump($array_new);
Success #stdin #stdout 0s 82560KB
stdin
Standard input is empty
stdout
array(3) {
  [0]=>
  array(3) {
    ["idApiUm"]=>
    int(123)
    ["title"]=>
    string(5) "Teste"
    ["idApiDois"]=>
    int(999)
  }
  [1]=>
  array(2) {
    ["idApiDois"]=>
    int(765)
    ["title"]=>
    string(7) "Título"
  }
  [2]=>
  array(2) {
    ["idApiUm"]=>
    int(632)
    ["title"]=>
    string(4) "Nome"
  }
}