2

How to get unique values in multidimensional array ? We have to get only unique values in multiple array in comma seprated. check below array

Array
(
    [0] => Array
        (
            [product_id] => BAT0002
            [vendor_id] => 1,2,3,4
        )

    [1] => Array
        (
            [product_id] => BAT0003
            [vendor_id] => 3
        )

    [2] => Array
        (
            [product_id] => BAT0004
            [vendor_id] => 1,2,3
        )

)

Code:-

$vendor_id = array();
foreach ($rfq_product_master_data_array as $rfq_data_array) 
{
    $product_id = $rfq_data_array['product_id'];
    $vendor_id[] = $rfq_data_array['vendor_id'];

}
$uniquePids = array_unique($vendor_id);
echo "<PRE>";
print_r($uniquePids);

Actual result:-

Array
(
    [0] => 1,2,3,4
    [1] => 3
    [2] => 1,2,3
)

Expected result:-

Array
    (
        [0] => 1,2,3,4
    )
2
  • What you mean by unique all are unique only [0] => 1,2,3,4 [1] => 3 [2] => 1,2,3 Commented Jun 10, 2019 at 6:12
  • I want this all unique array merge and get only unique value Commented Jun 10, 2019 at 6:16

3 Answers 3

2

you have to do below modification in foreach() and code after that:

foreach ($rfq_product_master_data_array as $rfq_data_array) 
{
    $product_id = $rfq_data_array['product_id'];

    $explodedArray = explode(',',$rfq_data_array['vendor_id']);
    foreach($explodedArray as $value){
        $vendor_id[$value] = $value;
    }
}
print_r($vendor_id); //so you can use it further directly.

//if you want comma separated string then

echo $uniquePids = implode(',',array_values($vendor_id));

Special Notes: it will give you comma separated string of all unique vendor_id in each case, like suppose if you have

Array
(
    [0] => 1,2,3,4
    [1] => 5
    [2] => 1,2,6,7
)

it will produce:- 1,2,3,4,5,6,7

Sign up to request clarification or add additional context in comments.

1 Comment

@Devidas check notes section. I have mentioned one case there. read it carefully and see that you want same output or not?
1

You can simply use built in array function:

$data = //your data array

$res1[] = implode(',',array_unique(explode(',',implode(',',array_column($data,"vendor_id")))));

//array_column : to get single column - vendor_id
//implode : to combine all vendor ids
//explode : to split ids into array
//array_unique : to get unique values
//implode : to combine unique values

Then result of all function set to $res1[] array at o index

var_dump($res1);

Expected resut in short code:

Array
(
    [0] => 1,2,3,4
)

Comments

0

Do like this:

$vendor_ids = array();
foreach ($rfq_product_master_data_array as $rfq_data_array) 
{
    $product_id = $rfq_data_array['product_id'];
    $vendor_ids = array_merge($vendor_ids, explode(",", $rfq_data_array['vendor_id']));
}
$uniquePids = array_unique($vendor_ids);
echo "<PRE>";
print_r($uniquePids);

1 Comment

Please never post code-only answers on Stack Overflow. Every answer should include an explanation with the intent to educate/empower the OP and thousands of future researchers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.