【问题标题】:how to combine two arrays values into one?如何将两个数组值合并为一个?
【发布时间】:2019-04-11 08:59:36
【问题描述】:

我试图在 schedules_id 等于请求 schedules_id 时获得预订座位,然后显示属于 schedules_id 的所有座位,但得到更多分隔数组的

插入

$booking               = new Bookings();
$booking->users_id     = 4;
$booking->schedules_id = $schedules_id;
$booking->buses_id     = $buses_id;
$booking->routes_id    = $routes_id;
$booking->seat         = implode(',', $seat);
$booking->price        = $request->price;
$booking->profile      = 'pending';

收集

class PassengersController extends Controller
{
    public function booking(Request $request)
    {
        //river is used to pass important params with flow of it from page to page
        $seat         = $request->seat;
        $buses_id     = $request->buses_id;
        $schedules_id = $request->schedules_id;
        $data         = Buses::where('buses_id', $buses_id)->first();
        $seat         = json_decode($data->seat_layout, true);
        $front        = json_decode($data->front_layout, true);
        $bookingSeat  = Bookings::whereColumn('schedules_id', 'schedules_id')->get();

        $bookingSeat = $bookingSeat->map(function ($bookSeat) {
            $bookSeat->seat = explode(",", $bookSeat->seat);
            return $bookSeat;
        });

        return view('frontend.booking', ['seat' => $seat, 'buses_id' => $buses_id, 'schedules_id' => $schedules_id, 'front' => $front, 'bookingSeet' => $bookingSeat]);

    }
}

表格

bookings_id users_id schedules_id buses_id routes_id seat price profile
    1           1         6           1       3        1  Null  pending
    2           1         6           1       3        2  Null  pending

我的数组如下所示:

#items: array:2 [▼

    0 => Bookings {#431 ▶}
        "bookings_id" => 1
        "users_id" => 1
        "schedules_id" => 6
        "buses_id" => 1
        "routes_id" => 3
        "seat" => "1"
        "price" => null
        "profile" => "pending"
        "created_at" => "2019-04-09 00:00:00"
        "updated_at" => "2019-04-09 00:00:00"
    1 => Bookings {#432 ▶}
        "bookings_id" => 2
        "users_id" => 1
        "schedules_id" => 6
        "buses_id" => 1
        "routes_id" => 3
        "seat" => "2"
        "price" => null
        "profile" => "pending"
        "created_at" => "2019-04-10 00:00:00"
        "updated_at" => "2019-04-10 00:00:00"

但我想要的是我不想一次又一次地复制相同的数据。问题是我的 booking.blade.php 显示像 2 个公共汽车座位布局。例如,如果一辆公共汽车有 50 个座位 我在 Blade.php 中获得 100 个席位

我的预期结果如下所示:

#items: array:1 [▼

0 => Bookings {#431 ▶}
        "bookings_id" => 1,2
        "users_id" => 1
        "schedules_id" => 6
        "buses_id" => 1
        "routes_id" => 3
        "seat" => "1","2"
        "price" => null
        "profile" => "pending"
        "created_at" => "2019-04-09 00:00:00"
        "updated_at" => "2019-04-09 00:00:00"

或任何其他建议对我更有帮助。
提前感谢

【问题讨论】:

  • 试试这个例子:php.net/manual/en/function.array-unique.php#116302 或简单的array_unique()
  • 键必须是唯一的,如果你把它们组合起来,你会失去除了最后一个之外的所有东西......这是事实,除非你删除键,否则它只是一团糟。
  • @SamiAhmedSiddiqui 试过但得到这个“array_unique() 期望参数 1 是数组,给定对象”
  • 它们不是重复的,你打算如何解决这个"created_at" => "2019-04-09 00:00:00""created_at" => "2019-04-10 00:00:00",你只是在“预期”结果中忽略了这一点,有几个“这些”,跨度>
  • @ArtisticPhoenix 是的,你说得对,我也想,所以我还有其他方法可以将座位数据传递到刀片或使用 ~profile~ 值吗?

标签: php laravel


【解决方案1】:

您要合并的不是两个数组,而是两个实体,每个实体代表您的预订表中的一行,在我看来,它们不应该合并,这是主要原因

  • 每个对象代表数据库中的一行,如果您按照您的意愿合并它们,您如何编辑一个?删除一个?等等……

  • 预订舱位就像一个“预订”的蓝图,而不是全部

  • 您不会“一次又一次地复制相同的数据”,您只需为具有不同值的不同对象获得相同的属性名称 :)

可能还有 100 多个原因,但它们是第一个来找我的 希望对你有帮助

【讨论】:

  • 那么我还有其他方法可以将座位数据传递到刀片或使用 ~profile~ 值吗?
猜你喜欢
  • 1970-01-01
  • 2021-11-03
  • 2021-07-16
  • 2012-11-19
  • 1970-01-01
  • 1970-01-01
  • 2020-12-15
相关资源
最近更新 更多