fork(1) download
  1. <?php
  2. class Product
  3. {
  4. public function __construct($ean, $price, $count){
  5. $this->ean = $ean;
  6. $this->productPrice = $price;
  7. $this->productCount = $count;
  8. }
  9.  
  10. public function __get($property) {
  11. if (property_exists($this, $property)) {
  12. return $this->$property;
  13. }
  14. }
  15.  
  16. }
  17.  
  18.  
  19. function removeduplicateKeys($data){
  20. $tmp = [];
  21. for($i=0; $i < count($data); $i++){
  22. if(!array_key_exists($data[$i]["ean"], $tmp)) {
  23. $tmp[$data[$i]["ean"]] = $data[$i];
  24. } else if ($data[$i]["productCount"] < $tmp[$data[$i]["ean"]]["productCount"]){
  25. $tmp[$data[$i]["ean"]]["productCount"] = $data[$i]["productCount"];
  26. }
  27.  
  28.  
  29. }
  30.  
  31. $products = [];
  32. foreach($tmp as $p){
  33. array_push($products, new Product($p["ean"],
  34. $p["productPrice"],
  35. $p["productCount"]));
  36. }
  37.  
  38.  
  39. return $products;
  40. }
  41.  
  42.  
  43. // $arr =array(
  44. // "0" => Array
  45. // (
  46. // "ean" => 6900532615069,
  47. // "productPrice" => 1140,
  48. // "productCount" => 5
  49. // ),
  50. // "1" => Array
  51. // (
  52. // "ean" => 6900532615069,
  53. // "productPrice" => 1140,
  54. // "productCount" => 50
  55. // ),
  56. // "2" => Array
  57. // (
  58. // "ean" => 6900535364122,
  59. // "productPrice" => 1140,
  60. // "productCount" => 50
  61. // ),
  62.  
  63. // "3" => Array
  64. // (
  65. // "ean" => 6900532615069,
  66. // "productPrice" => 1140,
  67. // "productCount" => 10,
  68. // ));
  69. $arr =array(
  70. "0" => Array
  71. (
  72. "ean" => 6900532615069,
  73. "productPrice" => 1140,
  74. "productCount" => 10,
  75. ),
  76. "1" => Array
  77. (
  78. "ean" => 6900532615069,
  79. "productPrice" => 1140,
  80. "productCount" => 50
  81. ),
  82. "2" => Array
  83. (
  84. "ean" => 6900535364122,
  85. "productPrice" => 1140,
  86. "productCount" => 50
  87. ),
  88. "3" => Array
  89. (
  90. "ean" => 6900535364122,
  91. "productPrice" => 1140,
  92. "productCount" => 500
  93. ),
  94. "4" => Array
  95. (
  96. "ean" => 6900535364122,
  97. "productPrice" => 1140,
  98. "productCount" => 0
  99. ),
  100. "5" => Array
  101. (
  102. "ean" => 6900532615069,
  103. "productPrice" => 1140,
  104. "productCount" => 10,
  105. ));
  106.  
  107. print_r(removeduplicateKeys($arr));
Success #stdin #stdout 0.02s 82560KB
stdin
Standard input is empty
stdout
Array
(
    [0] => Product Object
        (
            [ean] => 6900532615069
            [productPrice] => 1140
            [productCount] => 10
        )

    [1] => Product Object
        (
            [ean] => 6900535364122
            [productPrice] => 1140
            [productCount] => 0
        )

)