yii\helpers\BaseArrayHelper::merge PHP Метод

merge() публичный статический Метод

If each array has an element with the same string key value, the latter will overwrite the former (different from array_merge_recursive). Recursive merging will be conducted if both arrays have an element of array type and are having the same key. For integer-keyed elements, the elements from the latter array will be appended to the former array. You can use [[UnsetArrayValue]] object to unset value from previous array or [[ReplaceArrayValue]] to force replace former value instead of recursive merging.
public static merge ( array $a, array $b ) : array
$a array array to be merged to
$b array array to be merged from. You can specify additional arrays via third argument, fourth argument etc.
Результат array the merged array (the original arrays are not changed.)
    public static function merge($a, $b)
    {
        $args = func_get_args();
        $res = array_shift($args);
        while (!empty($args)) {
            $next = array_shift($args);
            foreach ($next as $k => $v) {
                if ($v instanceof UnsetArrayValue) {
                    unset($res[$k]);
                } elseif ($v instanceof ReplaceArrayValue) {
                    $res[$k] = $v->value;
                } elseif (is_int($k)) {
                    if (isset($res[$k])) {
                        $res[] = $v;
                    } else {
                        $res[$k] = $v;
                    }
                } elseif (is_array($v) && isset($res[$k]) && is_array($res[$k])) {
                    $res[$k] = self::merge($res[$k], $v);
                } else {
                    $res[$k] = $v;
                }
            }
        }
        return $res;
    }

Usage Example

Пример #1
0
 public function actionIndex($search)
 {
     if ($search != null && $search != '') {
         $search = htmlspecialchars($search, ENT_HTML5);
         $activitySearchModel = new ActivitySearchGlobal();
         $activitySearchModel->globalSearch = $search;
         $activitySearchModelDataProvider = $activitySearchModel->search(Yii::$app->request->queryParams);
         $outdoorAttractionSearchModel = new OutsideAttractionSearchGlobal();
         $outdoorAttractionSearchModel->globalSearch = $search;
         $outdoorAttractionDataProvider = $outdoorAttractionSearchModel->search(Yii::$app->request->queryParams);
         $dataProvider = BaseArrayHelper::merge($activitySearchModelDataProvider, $outdoorAttractionDataProvider);
         if (Yii::$app->user->can('postIndex')) {
             $postSearchModel = new PostSearchGlobal();
             $postSearchModel->globalSearch = $search;
             $postDataProvider = $postSearchModel->search(Yii::$app->request->queryParams);
             $dataProvider = BaseArrayHelper::merge($dataProvider, $postDataProvider);
             $postReplySearchModel = new PostReplaySearchGlobal();
             $postReplySearchModel->globalSearch = $search;
             $postReplyDataProvider = $postReplySearchModel->search(Yii::$app->request->queryParams);
             $dataProvider = BaseArrayHelper::merge($dataProvider, $postReplyDataProvider);
         }
         if (Yii::$app->user->can('announcementIndex')) {
             $announcementSearchModel = new AnnouncementSearchGlobal();
             $announcementSearchModel->globalSearch = $search;
             $announcementDataProvider = $announcementSearchModel->search(Yii::$app->request->queryParams);
             $dataProvider = BaseArrayHelper::merge($dataProvider, $announcementDataProvider);
         }
         return $this->render('index', ['input' => $search, 'result' => $dataProvider]);
     } else {
         throw new ForbiddenHttpException(Yii::t('yii', 'Input something'));
     }
 }
All Usage Examples Of yii\helpers\BaseArrayHelper::merge