John Davidson

convert array of arrays into comma separated list with unique values - PHP

0 comments
Message:


I have an array that looks like below:


$var[] =   ^ array:4 [▼
0 => array:1 [▼
0 => "apple"
]
1 => array:1 [▼
0 => "apple"
1 => "grape"
]
2 => array:1 [▼
0 => "orange"
]
3 => array:1 [▼
0 => "banana"
]
]

Is there a way to convert this array into comma separated list with unique values as below?


$var = "apple,grape,orange,banana"

I tried implode(', ',$var) but it's not working as expected. Any help pls?


Update: My sub-array may have more than 1 element. It's dynamic.



My Answer:
array:3 [▼
0 => array:2 [▼
0 => "apple"
1 => "banana"
]
1 => array:2 [▼
0 => "orange"
1 => "apple"
]
2 => array:2 [▼
0 => "banana"
1 => "grapes"
]
]

I want to convert this array of arrays into a comma-separated list with unique values. The expected output should be:

"apple, banana, orange, grapes"

Rate this post

3 of 5 based on 5000 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us