Reputation: 337
I am extracting duplicated values from nested arrays. I would like to delete these extraceted items from the $bigarray. would you give me some ideas...?
$bigarray = array(
"430" => array('milk', 'turky', 'apple'),
"433" => array('milk', 'apple', 'orange', 'england'),
"444" => array('milk', 'apple', 'orange', 'spain')
);
$intersected = null;
foreach ($bigarray as $arr) {
$intersected = $intersected ? array_intersect($arr, $intersected) : $arr;
if (!$intersected) {
break; // no reason to continue
}
}
foreach ($intersected as $inter){
foreach ($bigarray as $arr) {
foreach ($arr as $value=>$key) {
if ($key == $inter){
unset($arr[$value]);
}
}
//print_r($arr);
}
}
print_r($bigarray );
Upvotes: 1
Views: 299
Reputation: 89
You can use the array_unique($array,[, int $sort_flags] function. if you don't specify the optional sort_flag, the function will compare values converting everything to string. if you have values other than string in the array, you can specify sort_flag to be one of the following values
SORT_REGULAR - compare items normally (don't change types)
SORT_NUMERIC - compare items numerically
SORT_STRING - compare items as strings
SORT_LOCALE_STRING - compare items as strings, based on the current locale.
Example from PHP.net
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
print_r($result);
for more information refer http://php.net/manual/en/function.array-unique.php
Upvotes: 0
Reputation: 1751
function array_unique_nested($arr=array(),$matched=array(),$cm=false){
foreach($arr as $i=>$v){
if (is_array($v)) {$arr[$i]=array_unique_nested($v,$matched,false);
$matched=array_unique_nested($v,$matched,true); continue;}
if (in_array($v,$matched)) {unset($arr[$i]);continue;}
$matched[]=$v;}
if ($cm) return $matched;
else return $arr;}
In case that doesn't work, this code snippet from http://php.net/manual/en/function.array-unique.php should do the trick.
if( !function_exists( 'array_flat' ) )
{
function array_flat( $a, $s = array( ), $l = 0 )
{
# check if this is an array
if( !is_array( $a ) ) return $s;
# go through the array values
foreach( $a as $k => $v )
{
# check if the contained values are arrays
if( !is_array( $v ) )
{
# store the value
$s[ ] = $v;
# move to the next node
continue;
}
# increment depth level
$l++;
# replace the content of stored values
$s = array_flat( $v, $s, $l );
# decrement depth level
$l--;
}
# get only unique values
if( $l == 0 ) $s = array_values( array_unique( $s ) );
# return stored values
return $s;
} # end of function array_flat( ...
}
Upvotes: 0
Reputation: 1027
I don't completely understood your question, but using array_unique() in your array I've got the following output:
array(1) {
[430]=>
array(3) {
[0]=>
string(4) "milk"
[1]=>
string(5) "turky"
[2]=>
string(5) "Apple"
}
}
Maybe this could be a way of acchieving what you want.
Upvotes: 0
Reputation: 15934
You should look at array_merge as it will merge 2 arrays together and only keep one duplicate. From the manual:
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
Sounds like homework and the question isn't very clear so that is all I can provide for now.
Upvotes: 1
Reputation: 4922
Is this what you're looking for?
foreach($bigarray as $id => $arr)
$bigarray[$id] = array_unique($arr);
Upvotes: 0