[PHP-NOTES] note 130263 added to function.array-intersect-key

[email protected] ("Anonymous")
Newsgroups php.notes
Message-ID <[email protected]>
May I suggest this array_intersect_key_recursive implementation ?

  function array_intersect_key_recursive(array $targetArray, array ...$comparedArrays): array
    {
        $targetArrayIntersected = array_intersect_key($targetArray, ...$comparedArrays);

        foreach (array_keys($targetArrayIntersected) as $key) {
            if (true === is_array($targetArray[$key])
                // all compared arrays have key with non-empty array as value
                && true === array_reduce(
                    $comparedArrays,
                    function (bool $isArrayPreviousSingleComparedArray, array $singleComparedArray) use ($key) {
                        return (true === $isArrayPreviousSingleComparedArray
                            && true === is_array($singleComparedArray[$key])
                            && false === empty($singleComparedArray[$key])
                        );
                    },
                    true)
                )
            {
                // repeat recursively
                $targetArrayIntersected[$key] = array_intersect_key_recursive(
                    $targetArray[$key], 
                    ...array_map(
                        function(array $singleComparedArray) use ($key): array {
                            return $singleComparedArray[$key];
                        },
                        $comparedArrays
                    )
                );
            }
        }
        return $targetArrayIntersected;
    }
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.137 (proxied: 217.70.85.141)
----
Manual Page -- https://php.net/manual/en/function.array-intersect-key.php
Edit        -- https://main.php.net/note/edit/130263
Del: integrated  -- https://main.php.net/note/delete/130263/integrated
Del: useless     -- https://main.php.net/note/delete/130263/useless
Del: bad code    -- https://main.php.net/note/delete/130263/bad+code
Del: spam        -- https://main.php.net/note/delete/130263/spam
Del: non-english -- https://main.php.net/note/delete/130263/non-english
Del: in docs     -- https://main.php.net/note/delete/130263/in+docs
Del: other reasons-- https://main.php.net/note/delete/130263
Reject      -- https://main.php.net/note/reject/130263
Search      -- https://main.php.net/manage/user-notes.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.