如何将两个数组合并为单个数组

时间:2020-05-22 05:21:26

标签: json laravel multidimensional-array

Array
(
    [0] => Array
        (
            [id] => 25
            [distance] => 0
        )

)
Array
(
    [0] => Array
        (
            [id] => 24
            [distance] => 0
        )

    [1] => Array
        (
            [id] => 27
            [distance] => 0
        )

    [2] => Array
        (
            [id] => 26
            [distance] => 14.568184704048
        )

)

我收到此响应是因为有多个位置(多个经度长),所以我得到了两个不同的数组,因此如何将它们全部合并为一个数组**

这是我的代码

if(!empty($ saveArray ['area'])&&!empty($ saveArray ['radius'])||!empty($ saveArray ['event_name_desc'])||!empty($ saveArray ['标签'])||!empty($ saveArray ['start_date'])||!empty($ saveArray ['end_date'])){

                $areas = explode('|',$saveArray['area']);
                // $query = $query->where(function($query) use($areas) {
                foreach($areas as $are) {
                    $query = Event::query();
                    $latlng = explode(',',$are);
                    $lat = $latlng[0];
                    $lng = $latlng[1];
                    $radius = $saveArray['radius'];
                    $query = $query->selectRaw(" id ,( 3959 * acos( cos( radians(?) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(?)) + sin( radians(?) ) * sin( radians( latitude ) ))) AS distance", [$lat, $lng, $lat])
                        ->having("distance", "<", $radius)
                        ->orderBy("distance",'asc')
                        ->offset(0)
                        ->limit(20);

                        if ( $saveArray['event_name_desc'] ) {
                            $query = $query->where('event_name','like','%'.$saveArray['event_name_desc'].'%');
                            $query = $query->orWhere('event_description','like','%'.$saveArray['event_name_desc'].'%');
                        }

                        if ( $saveArray['tags'] )  {
                            $tags = explode(',',$saveArray['tags']);
                            $query = $query->where(function($query) use($tags) {
                                foreach($tags as $tag) {
                                    $query->orWhere('tags', 'like', "%$tag%");
                                };
                            })
                            ->inRandomOrder();
                        }

                        $events = $query->get();
                        $event = json_decode(json_encode($events), true);

                        $record = [];
                        foreach ($event as $ls) {
                            $listing['id'] = (String)$ls['id'];
                            $record[] = $listing;
                        }
                        // $oneDimensionalArray = array_map('current', $record);
                        print_r($event);
                        // $multiplied = $events->map(function ($item, $key) {
                        // return $item->id;
                        // })->flatten();
                        // print_r($multiplied);
                    }

0 个答案:

没有答案