Home php combine the values ​​in the PHP array and create a new

combine the values ​​in the PHP array and create a new

Author

Date

Category

Tell me how to solve this problem:

There is an array derived from database:

Array (3) {
[0] = & gt; Array (14) {[id "] = & gt; String (1) "1" ["userhash"] = & gt; String (3) "Apple" ["ServicType"] = & gt; String (2) "11" ["Prise"] = & gt; String (1) "300"}
[1] = & gt; Array (14) {[id "] = & gt; String (1) "5" ["userhash"] = & gt; String (3) "PEAR" ["ServiceType"] = & gt; String (1) "5" ["Prise"] = & gt; String (1) "20"}
[2] = & gt; Array (14) {[id "] = & gt; String (1) "2" ["UserHash"] = & gt; String (3) "Apple" ["ServicType"] = & gt; String (2) "12" ["Prise"] = & gt; String (1) "200"}
}

How inside this array to know if there are repeated values ​​(in this example it is Apple), and then create a new array where to write down all the unique value, and also leave only one Apple with the combined price 500 and supplemented with values ​​in SERVICETYPE (Put values ​​from both arrays) so that it happens like this:

array (2) {
  [0] = & gt; Array (14) {[id "] = & gt; String (1) "1" ["userhash"] = & gt; String (3) "Apple" ["ServicType"] = & gt; String (5) "11, 12" ["Prise"] = & gt; String (1) "500"}
[1] = & gt; Array (14) {[id "] = & gt; String (1) "5" ["userhash"] = & gt; String (3) "PEAR" ["ServiceType"] = & gt; String (1) "5" ["Prise"] = & gt; String (1) "20"}

}

If you do in the cycle, all values ​​are compared with themselves, which does not solve the problem.

Tell me, please, code logic or example of a function to solve this task?


Answer 1, Authority 100%

Try to use GROUP BY in your database request. Thus, you exclude the repetition of columns

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions