3

I have an array like below. This is coming from a centralised database and I have no way of knowing beforehand what the actual array will contain. I want to compare the sub-arrays on keys and that is why want to delete the keys which are not present is all sub-arrays.

Array (
    [0] => Array
        (
            [a] => 
            [b] => 8
            [c] => 1
            [d] => taille-8
            [e] => 
            [k] => taill
        )

    [1] => Array
        (
            [a] => 
            [b] => 7
            [c] => 2
            [d] => taille-7
            [f] =>
            [k] => tafefef
        )

    [2] => Array
        (
            [a] => ce
            [b] => 34
            [c] => 2
            [d] => taille-34
            [g] => dee
            [k] => tacefef
        ) );

I want to delete the keys which are not repeating in all sub-arrays. In the above example they are 'e', 'f' and 'g'. This needs to happen dynamically.

Array (
    [0] => Array
        (
            [a] => 
            [b] => 8
            [c] => 1
            [d] => taille-8
            [k] => taill
        )

    [1] => Array
        (
            [a] => 
            [b] => 7
            [c] => 2
            [d] => taille-7
            [k] => tafefef
        )

    [2] => Array
        (
            [a] => ce
            [b] => 34
            [c] => 2
            [d] => taille-34
            [k] => tacefef            
        ) );

Any suggestion is appreciated.

3
  • How do you create the first array. if you creating it you can ignore those keys right? did you try that? Commented Aug 24, 2018 at 5:49
  • Have a look at php.net/manual/en/function.array-intersect-key.php Commented Aug 24, 2018 at 5:53
  • @LahiruMadusanka The array is coming from a database which is getting updated regularly. Not in my control and also I cannot know beforehand which keys will be there. Commented Aug 24, 2018 at 5:58

2 Answers 2

3

You can use argument unpacking. If your array is stored in $a, then the following should work:

// create an array with only the keys that are common to all subarrays
$new = array_intersect_key(...$a);

// prune original array
foreach ($a as &$arr) {
    $arr = array_intersect_key($new, $arr);
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can splice the array and loop the rest.
In the loop I overwrite the $new with whatever is the same in each iteration.
The end should be what is matching throughout the full array.

Then we need to use the new and as a template to remove the other items in the array, so we loop again and intersect again but overwrite $arr this time.

$arr =[['a' => 1, 'b' => 2, 'c' => 3],
       ['a' => 1, 'c' => 3],
       ['c' => 3]];

$new = $arr[0];

foreach($arr as $sub){
    $new = array_intersect_key($new, $sub);
}
foreach($arr as &$sub){
    $sub =array_intersect_key($sub, $new);
}
unset($sub);
Var_dump($arr);

Output

array(3) {
  [0]=>
  array(1) {
    ["c"]=>
    int(3)
  }
  [1]=>
  array(1) {
    ["c"]=>
    int(3)
  }
  [2]=>
  &array(1) {
    ["c"]=>
    int(3)
  }
}

https://3v4l.org/oM3M1l

3 Comments

Thank you for you answer. I do not want to change the actual structure of the array. So the result i was should look like $new =[ ['c' => 3], ['c' => 3], ['c' => 3]];
In that case I think we need to loop twice see updated code.
@sam11 No problem!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.