Multidimensional array, delete the array where the key and value correspond to another array

I want to remove duplicates where either the dimension or altunit matches another array, but ignore if they are empty.

Array ( [0] => Array ( [id] => 2 [altunit] => % [measurement] => ) [1] => Array ( [id] => 3 [altunit] => [measurement] => 6 ) [2] => Array ( [id] => 4 [altunit] => % [measurement] => ) [3] => Array ( [id] => 5 [altunit] => [measurement] => 6 ) [4] => Array ( [id] => 6 [altunit] => [measurement] => 6 ) ) 

becomes

 Array ( [0] => Array ( [id] => 2 [altunit] => % [measurement] => ) [1] => Array ( [id] => 3 [altunit] => [measurement] => 6 ) ) 

The best I can come up with is:

 $test = array ( 0 => array ( 'id' => '2', 'altunit' => '%', 'measurement' => NULL, ), 1 => array ( 'id' => '3', 'altunit' => NULL, 'measurement' => '6', ), 2 => array ( 'id' => '4', 'altunit' => NULL, 'measurement' => '6', ), 3 => array ( 'id' => '5', 'altunit' => NULL, 'measurement' => '6', ), 4 => array ( 'id' => '6', 'altunit' => NULL, 'measurement' => '6', ), ); $num = []; foreach($test as $k => $v) $num[] = $v['measurement']; 

But this only works for measurement and removes the id and altunit keys.

+6
source share
6 answers

Hamm, Create an array of "known value" for the dimension and altunit, and then check if it exists for the rest of the values.

sort of:

 $knowed_altunit=array(); $knowed_measurement=array(); foreach($test as $k=>$v){ if(in_array($v['altunit'],$knowed_altunit) || in_array($v['mesurement'],$knowed_measurement)){ //if the value of altunit or measurement is already knowed then remove the entry from the array, unset($test[$k]); }else{ //if it never been seen, add it so further entry can be checked agaisnt the knowed value $knowed_altunit[]=$v['altunit']; $knowed_measurement[]=$v['mesurement']; } } 

Sorry if the typo, but thins, can help you wrap your head around solving your problem.

+3
source

You can try this code:

 <?php /* before you need to check that $test variable is declarated and have all items to check */ $values_altunit = array(); $values_measurement = array(); $result = array(); foreach($test as $key => $value) { /* check if exist altunit and measurement values in blacklist arrays */ if (!in_array($value['altunit'], $values_altunit) && !in_array($value['measurement'], $values_measurement)) { /* if not exist, add the item to $result array */ $result[$key] = $value; /* and add altunit and measurement values to blacklist arrays */ $values_altunit[] = $value['altunit']; $values_measurement[] = $value['measurement']; } } /* print result items */ var_dump($result); ?> 
+2
source

This is a short code that can produce results for unique measurements.

  <?php $arr = array( "0"=> array ( "id" => 2, "altunit" => "%", "measurement" => "", ), "1"=> array ( "id" => 3, "altunit" => "", "measurement" => 6, ), "2"=> array ( "id" => 4, "altunit" => "%", "measurement" => "", ), "3"=> array ( "id" => 5, "altunit" => "", "measurement" => 6, ), "4"=> array ( "id" => 6, "altunit" => "", "measurement" => 6, ) ); $unique_measure = $new_array = array(); foreach($arr as $sup_key => $sup_val){ foreach($sup_val as $sub_key => $sub_val){ if(!in_array($sup_val['measurement'], $unique_measure)){ array_push($unique_measure, $sup_val['measurement']); array_push($new_array,$sup_val); } } } print_r($new_array); ?> 

Output:

  Array ( [0] => Array ( [id] => 2 [altunit] => % [measurement] => ) [1] => Array ( [id] => 3 [altunit] => [measurement] => 6 ) ) 

try this code. he can do the trick.

Another approach to solving problems can remove this specific key from the main array, using unset(your_array_key) will do the trick in the same code.

+1
source

Try it, it can help you.

 function remove_dup($array, $keys ) { $out = array(); foreach($array as $sub) { if(empty($out)) { $out[] = $sub; continue; } foreach($keys as $key) { if($flag=in_array( $sub[$key],array_map(function($e) use($key){ return $e[$key];}, $out)) ) break; } if(!$flag) $out[] = $sub; } return $out; } // Usage print_r( remove_dup($array, array('altunit','measurement') ) ); 

Test

 [ akshay@localhost tmp]$ cat test.php <?php function remove_dup($array, $keys ) { $out = array(); foreach($array as $sub) { if(empty($out)) { $out[] = $sub; continue; } foreach($keys as $key) { if($flag=in_array( $sub[$key],array_map(function($e) use($key){ return $e[$key];}, $out)) ) break; } if(!$flag) $out[] = $sub; } return $out; } $array = array( "0"=> array ( "id" => 2, "altunit" => "%", "measurement" => "", ), "1"=> array ( "id" => 3, "altunit" => "", "measurement" => 6, ), "2"=> array ( "id" => 4, "altunit" => "%", "measurement" => "", ), "3"=> array ( "id" => 5, "altunit" => "", "measurement" => 4, ), "4"=> array ( "id" => 6, "altunit" => "", "measurement" => 6, ) ); print_r( remove_dup($array, array('altunit','measurement') ) ); ?> 

Output

 [ akshay@localhost tmp]$ php test.php Array ( [0] => Array ( [id] => 2 [altunit] => % [measurement] => ) [1] => Array ( [id] => 3 [altunit] => [measurement] => 6 ) ) 
+1
source

What you really want is set , not an array. Therefore, if you cannot fix the way you build the array in the first place (I assume that it came from an SQL query, for which there would be much less code to fix), you have two options for creating mapped sets in PHP.

  • You can use SplObjectStorage
  • You can use Array with a key as a serialized set view

The first approach would look something like this:

 $set = new SplObjectStorage(); $arr = [ 0 => [ 'id' => '2', 'altunit' => '%', 'measurement' => NULL, ], 1 => [ 'id' => '3', 'altunit' => NULL, 'measurement' => '6', ], 2 => [ 'id' => '4', 'altunit' => NULL, 'measurement' => '6', ], 3 => [ 'id' => '5', 'altunit' => NULL, 'measurement' => '6', ], 4 => [ 'id' => '6', 'altunit' => NULL, 'measurement' => '6', ], ]; foreach($arr as $part) { if (isset($part['altunit'])) { // ignore if blank $key = (object) $part; $set->attach($key); // attach the set } } // Now you have... foreach($set as $value) { var_dump($value); } 

It will give you ...

 object(stdClass)#2 (3) { ["id"]=> string(1) "2" ["altunit"]=> string(1) "%" ["measurement"]=> NULL } 

The second approach, using an array, where the array key represents a serialized version of the set, would look something like this:

 $set = []; $arr = [ 0 => [ 'id' => '2', 'altunit' => '%', 'measurement' => NULL, ], 1 => [ 'id' => '3', 'altunit' => NULL, 'measurement' => '6', ], 2 => [ 'id' => '4', 'altunit' => NULL, 'measurement' => '6', ], 3 => [ 'id' => '5', 'altunit' => NULL, 'measurement' => '6', ], 4 => [ 'id' => '6', 'altunit' => NULL, 'measurement' => '6', ], ]; foreach($arr as $part) { if (isset($part['altunit'])) { // ignore if blank $key = "{$part['altunit']}\0{$part['measurement']}"; $set[$key] = $part; // attach the set } } // Now you have... foreach($set as $value) { var_dump($value); } 

And that will give you ...

 array(3) { ["id"]=> string(1) "2" ["altunit"]=> string(1) "%" ["measurement"]=> NULL } 

NB

But, if this is the result of a set from an SQL query, it is possible you could just more efficiently eliminate the deduplication process completely by modifying the query to use the WHERE NOT NULL and GROUP BY clauses instead.

0
source
 You can try this also <?php $keys = array_map(function ($i) { return $i; }, $array); $dumparr = array_combine($keys, $array); $result = array_values($deduped); print_r($result); ?> 
0
source

All Articles