Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-dimensional array, remove array where key and value match another array

I want to remove duplicates where either measurement or altunit matches another array, but ignoring if they're blank.

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
        )

)

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.

like image 861
ditto Avatar asked Jul 13 '15 04:07

ditto


People also ask

How to select an element from a multidimensional array?

PHP - Multidimensional Arrays 1 For a two-dimensional array you need two indices to select an element 2 For a three-dimensional array you need three indices to select an element More ...

What are two-dimensional arrays?

As seen in an example of the array above, two-dimensional arrays are the arrays in which each element is another array. In simple language, we can say that two dimensional arrays are an array of arrays. To properly understand the concept of two dimensional array, let use developer tools for it.

How to declare a multidimensional array in C?

The simplest form of multidimensional array is the two-dimensional array. A two-dimensional array is, in essence, a list of one-dimensional arrays. To declare a two-dimensional integer array of size [x] [y], you would write something as follows − Where type can be any valid C data type and arrayName will be a valid C identifier.

What are the methods in multi dimensional array in JavaScript?

Top 8 Methods in Multi-Dimensional Array in JavaScript 1 Pop ( ) 2 Push ( ) 3 Sort ( ) 4 Reverse ( ) 5 IndexOf ( ) 6 Shift ( ) 7 Unshift ( ) 8 Splice ( ) More ...


1 Answers

Humm, Make an array of 'knowed value' for measurement and altunit and then check it it exist on the rest of the values.

something like:

$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 any typo but thins might help you wrap your head around the solution to your problem.

like image 71
Louis Loudog Trottier Avatar answered Oct 10 '22 21:10

Louis Loudog Trottier