ing $parentKey The parent key. * @return array The intersected array. */ public function arrayIntersectRecursive( $array1, $array2, $allowedKey = '', $parentKey = '' ) { if ( ! $allowedKey || $allowedKey === $parentKey ) { $array1 = $this->arrayIntersectRecursiveHelper( $array1, $array2 ); } if ( empty( $array1 ) ) { return []; } foreach ( $array1 as $k => $v ) { if ( is_array( $v ) && isset( $array2[ $k ] ) ) { $array1[ $k ] = $this->arrayIntersectRecursive( $array1[ $k ], $array2[ $k ], $allowedKey, $k ); } } if ( $this->isArrayNumeric( $array1 ) ) { $array1 = array_values( $array1 ); } return $array1; } /** * Recursively intersects the two given arrays. Supports arrays with a mix of nested arrays and primitive values. * Helper function for arrayIntersectRecursive(). * * @since 4.5.4 * * @param array $array1 The first array. * @param array $array2 The second array. * @return array The intersected array. */ private function arrayIntersectRecursiveHelper( $array1, $array2 ) { if ( null === $array2 ) { $array2 = []; } if ( is_array( $array1 ) ) { // First, check with keys are nested arrays and which are primitive values. $arrays = []; $primitives = []; foreach ( $array1 as $k => $v ) { if ( is_array( $v ) ) { $arrays[ $k ] = $v; } else { $primitives[ $k ] = $v; } } // Then, intersect the primitive values. $intersectedPrimitives = array_intersect_assoc( $primitives, $array2 ); // Finally, recursively intersect the nested arrays. $intersectedArrays = []; foreach ( $arrays as $k => $v ) { if ( isset( $array2[ $k ] ) ) { $intersectedArrays[ $k ] = $this->arrayIntersectRecursiveHelper( $v, $array2[ $k ] ); } else { // If the nested array doesn't exist in the second array, we can just unset it. unset( $arrays[ $k ] ); } } // Merge the intersected arrays and primitive values. return array_merge( $intersectedPrimitives, $intersectedArrays ); } return array_intersect_assoc( $array1, $array2 ); } /** * Sorts the keys of an array alphabetically. * The array is passed by reference, so it's not returned the same as in `ksort()`. * * @since 4.4.0.3 * * @param array $array The array to sort, passed by reference. */ public function arrayRecursiveKsort( &$array ) { foreach ( $array as &$value ) { if ( is_array( $value ) ) { $this->arrayRecursiveKsort( $value ); } } ksort( $array ); } /** * Creates a multidimensional array from a list of keys and a value. * * @since 4.5.3 * * @param array $keys The keys to create the array from. * @param mixed $value The value to assign to the last key. * @param array $array The array when recursing. * @return array The multidimensional array. */ public function createMultidimensionalArray( $keys, $value, $array = [] ) { $key = array_shift( $keys ); if ( empty( $array[ $key ] ) ) { $array[ $key ] = null; } if ( 0 < count( $keys ) ) { $array[ $key ] = $this->createMultidimensionalArray( $keys, $value, $array[ $key ] ); } else { $array[ $key ] = $value; } return $array; } }