Hello, after print my query

print_r ($code_2);

I have received the result which has the duplicate array. Please help me to merge them! Thanks.

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [name] => admin
            [phone] => 353534545
            [notes] => 
        )

)


Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [name] => admin
            [phone] => 353534545
            [notes] => 
        )

)


Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [name] => admin
            [phone] => 353534545
            [notes] => 
        )

)


Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [name] => admin
            [phone] => 353534545
            [notes] => 
        )

)


Array
(
    [0] => stdClass Object
        (
            [id] => 9
            [name] => HTC
            [phone] => 5464565
            [notes] => 
        )

)


Array
(
    [0] => stdClass Object
        (
            [id] => 9
            [name] => HTC
            [phone] => 5464565
            [notes] => 
        )

)


Array
(
    [0] => stdClass Object
        (
            [id] => 9
            [name] => HTC
            [phone] => 5464565
            [notes] => 
        )

)


Array
(
    [0] => stdClass Object
        (
            [id] => 12
            [name] => Test 1
            [phone] => 024345346
            [notes] => 
        )

)


Array
(
    [0] => stdClass Object
        (
            [id] => 9
            [name] => HTC
            [phone] => 5464565
            [notes] => 
        )

)


Array
(
    [0] => stdClass Object
        (
            [id] => 9
            [name] => HTC
            [phone] => 5464565
            [notes] => 
        )

)


Array
(
    [0] => stdClass Object
        (
            [id] => 9
            [name] => HTC
            [phone] => 5464565
            [notes] => 
        )

)


Array
(
    [0] => stdClass Object
        (
            [id] => 17
            [name] => ABDC
            [phone] => 545656
            [notes] => 
        )

)


Array
(
    [0] => stdClass Object
        (
            [id] => 9
            [name] => HTC
            [phone] => 5464565
            [notes] => 
        )

)


Array
(
    [0] => stdClass Object
        (
            [id] => 9
            [name] => HTC
            [phone] => 5464565
            [notes] => 
        )

)


Array
(
    [0] => stdClass Object
        (
            [id] => 9
            [name] => HTC
            [phone] => 5464565
            [notes] => 
        )

)


Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [name] => admin
            [phone] => 353534545
            [notes] => 
        )

)


Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [name] => admin
            [phone] => 353534545
            [notes] => 
        )

)


Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [name] => admin
            [phone] => 353534545
            [notes] => 
        )

)


Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [name] => admin
            [phone] => 353534545
            [notes] => 
        )

)


Array
(
    [0] => stdClass Object
        (
            [id] => 18
            [name] => ABCDE
            [phone] => 3453463456
            [notes] => 
        )

)


Array
(
    [0] => stdClass Object
        (
            [id] => 20
            [name] => demo
            [phone] => 5675454
            [notes] => 
        )

)


Array
(
    [0] => stdClass Object
        (
            [id] => 20
            [name] => demo
            [phone] => 5675454
            [notes] => 
        )

)


Array
(
    [0] => stdClass Object
        (
            [id] => 20
            [name] => demo
            [phone] => 5675454
            [notes] => 
        )

)


Array
(
    [0] => stdClass Object
        (
            [id] => 20
            [name] => demo
            [phone] => 5675454
            [notes] => 
        )

)


Array
(
    [0] => stdClass Object
        (
            [id] => 20
            [name] => demo
            [phone] => 5675454
            [notes] => 
        )

)


Array
(
    [0] => stdClass Object
        (
            [id] => 20
            [name] => demo
            [phone] => 5675454
            [notes] => 
        )

)


Array
(
    [0] => stdClass Object
        (
            [id] => 20
            [name] => demo
            [phone] => 5675454
            [notes] => 
        )

)


Array
(
    [0] => stdClass Object
        (
            [id] => 20
            [name] => demo
            [phone] => 5675454
            [notes] => 
        )

)


Array
(
    [0] => stdClass Object
        (
            [id] => 20
            [name] => demo
            [phone] => 5675454
            [notes] => 
        )

)

If your query comes back with multiple arrays like this, then you probably need to refactor your query, rather than cleaning up the results.

If there's really no other way, or really need to merge arrays, I've used the following in the past to do this.

array_unique(array_merge($array1,$array2), SORT_REGULAR);

http://php.net/manual/en/function.array-merge.php
http://php.net/manual/en/function.array-unique.php

One more thing to add to this, that solution to merge arrays and make unique will most likely fail on complex array data.
It would require a more complex solution, or a refactoring of the query.

Member Avatar for diafol

I agree, the array, as is, is nonsensical. Looks like you pulled this from a DB without using an appropriate filter.

I have used the loop for $i because I have selected datas from three different tables. Then the final results are looking like this one.
Step 1: SELECT id FROM ab_staff WHERE wp_user_id = "' .$user_id. '"
Step 2: SELECT id FROM ab_appointment WHERE staff_id = 'step 1'
Step 3: SELECT customer_id FROM ab_customer_appointment WHERE appointment_id = 'step 2'

Thank you so much! I will try it :)

After joining the table, if you still found you data having duplicated info, maybe you should consider goes for 'DISTINCT' selection also.

Hi,

This is my new query:

SELECT * FROM ab_customer INNER JOIN ab_customer_appointment ON ab_customer.id = ab_customer_appointment.appointment_id INNER JOIN ab_appointment ON ab_customer_appointment.appointment_id = ab_appointment.staff_id INNER JOIN ab_staff ON ab_appointment.staff_id = ab_staff.wp_user_id WHERE wp_user_id = "' .$user_id. '"

But, its result with

print_r

is empty

Array ( ) 

Help me, please.

FINALLY, I've success with

SELECT DISTINCT * FROM ab_customer 
WHERE id IN (SELECT customer_id FROM ab_customer_appointment WHERE appointment_id IN
(SELECT id FROM ab_appointment WHERE staff_id IN
(SELECT id FROM ab_staff WHERE wp_user_id = "' .$user_id. '")))

Thank you!

That looks like this:

SELECT DISTINCT c.* 
FROM ab_staff s, ab_appointment a, ab_customer_appointment ca, ab_customer c
WHERE s.wp_user_id = $user_id
AND a.staff_id = s.id
AND ca.appointment_id = a.id
AND c.id = ca.customer_id
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.