PHP - Mutidimensional array diff

前端 未结 3 1077
忘掉有多难
忘掉有多难 2021-01-14 20:43

i would like to ask for your help since I\'m having difficulty resolving this matter. I had created a function to facilitate on array diff but it does not suffice to my need

3条回答
  •  悲哀的现实
    2021-01-14 21:29

    This should do it, assuming all keys occur in both arrays:

    $diff = array();
    foreach ($session as $key => $values) {
        $diff[$key] = array_diff($values, $post[$key]);
    }
    

    Or, because I'm bored and array_map is underused:

    $diff = array_combine(
        array_keys($session),
        array_map(function ($a, $b) { return array_diff($a, $b); }, $session, $post)
    );
    

    (Assumed well ordered arrays though.)

提交回复
热议问题