Seems pretty simple: extract all pid values into their own array, run it through array_unique:
$uniquePids = array_unique(array_map(function ($i) { return $i['pid']; }, $holder));
The same thing in longhand:
$pids = array();
foreach ($holder as $h) {
$pids[] = $h['pid'];
}
$uniquePids = array_unique($pids);
Answer from deceze on Stack OverflowSeems pretty simple: extract all pid values into their own array, run it through array_unique:
$uniquePids = array_unique(array_map(function ($i) { return $i['pid']; }, $holder));
The same thing in longhand:
$pids = array();
foreach ($holder as $h) {
$pids[] = $h['pid'];
}
$uniquePids = array_unique($pids);
In php >= 5.5 you can use array_column:
array_unique(array_column($holder, 'pid'));
array_unique is using string conversion before comparing the values to find the unique values:
Note: Two elements are considered equal if and only if
(string) $elem1 === (string) $elem2. In words: when the string representation is the same. The first element will be used.
But an array will always convert to Array:
var_dump("Array" === (string) array());
You can solve this by specifying the SORT_REGULAR mode in the second parameter of array_unique:
$unique = array_unique($a, SORT_REGULAR);
Or, if that doesn’t work, by serializing the arrays before and unserializing it after calling array_unique to find the unique values:
$unique = array_map('unserialize', array_unique(array_map('serialize', $a)));
Here :)
<?php
$a = array (
0 => array ( 'value' => 'America', ),
1 => array ( 'value' => 'England', ),
2 => array ( 'value' => 'Australia', ),
3 => array ( 'value' => 'America', ),
4 => array ( 'value' => 'England', ),
5 => array ( 'value' => 'Canada', ),
);
$tmp = array ();
foreach (
row)
if (!in_array(
tmp)) array_push(
row);
print_r ($tmp);
?>
if $mainArray = array(2)... , try this code:
$mainString = "";
foreach($mainArray as $array){
foreach($array as $row){
$mainString .= $row;
} $mainString .= ", ";
}
$tempArray = explode(", ", $mainString);
$finishArray = array_unique($tempArray);
Try array_unique. This function takes one array as argument and generates a new array with distinct values of the argument array.
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
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
)
$result = array_reduce($array, function (array $result, array $item) {
return array_filter($result) + $item;
}, []);
This will probably do what you want (which is slightly unclear).
Explanation: It goes through each of your items one by one; it filters all empty values out of it, leaving only populated keys (array_filter); it then adds all keys which don't already exist (+) from the next item to it (read up on array_reduce). The end result should be an array with all non-empty keys from all the arrays merged into one, with the value being the first non-empty value encountered within the loop.
I know there is already a solution for this. Nonetheless, I want to present you a lightweight approach in terms of execution time and with the same functionality.
# your data here
$array = [
[
'id' => '123',
'headline' => 'one two three',
'body' => 'somebody',
'title_generic' => '',
'text_generic' => '',
],
[
'id' => '123',
'headline' => 'one two three',
'body' => null,
'title_generic' => 'title',
'text_generic' => 'text',
],
];
# the aggregate to be created
$aggregate = [];
foreach ($array as $el) {
if (empty($el)) continue;
foreach ($el as $k => $v) {
if (empty($v)) continue;
if (!isset($aggregate[$k])) {
$aggregate[$k] = $v;
}
}
}
# debug print
echo '<pre>';print_r($aggregate);echo '<pre>';
# the output
Array
(
[id] => 123
[headline] => one two three
[body] => somebody
[title_generic] => title
[text_generic] => text
)