0

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] => 
        )

)
5
Contributors
11
Replies
48
Views
2 Years
Discussion Span
Last Post by paul-lucas
Featured Replies
  • 1

    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 Read More

0

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.

0

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

0

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'

0

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

0

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.

0

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!

1

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
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.