Reputation: 95
Please help me on how to group the value from foreach result:
foreach ($result as $value) {
$group = $value['Barcode'];
echo $group.'<br>';
}
the result of this:
9822550005004 9822550005004 9844660005002 9844660005002 9844660005002 9844660005002
My expected result would be:
9822550005004 9844660005002
Upvotes: 1
Views: 5386
Reputation: 9583
You can use foreach and external array for getting the output like you want.
Using the foreach loop you need to store the each value in an array, here i store the value to the $arr
array and makes the key as same as the value, cause yuo need the unique values, after storing the values just implode them with suitable delimiter space
and get the desire output.
$arr = array();
foreach($result as $value){
$arr[$value['Barcode']] = $value['Barcode'];
}
echo implode(" ", $arr); //9822550005004 9844660005002
Using Array functions...
array_column
Get all the columns from the array as name Barcode
and makes anther array of them, After that array_unique
choose the unique values from the returned array and also make another array of it. So now you need to implode them as you want. The implode method makes the array as string with a delimiter. Here i use space
.
$arr = array_unique(array_column($result, "Barcode"));
echo implode(" ", $arr); //9822550005004 9844660005002
Upvotes: 2