【问题标题】:Get particular values multidimensional array Laravel获取特定值多维数组 Laravel
【发布时间】:2022-01-02 18:10:46
【问题描述】:

我想从#attributes 字段(如"title""location")中获取某些字段的值,以便填写日历。

dd($events); 输出:

Illuminate\Database\Eloquent\Collection {#948 ▼
  #items: array:3 [▼
    "26-01-2021" => Illuminate\Database\Eloquent\Collection {#972 ▶}
    "01-02-2021" => Illuminate\Database\Eloquent\Collection {#962 ▶}
    "03-11-2021" => Illuminate\Database\Eloquent\Collection {#965 ▼
      #items: array:1 [▼
        0 => App\Models\DaysEvent {#994 ▼
          #table: "days_events"
          #casts: array:11 [▶]
          #dates: array:2 [▶]
          #fillable: array:25 [▶]
          #dispatchesEvents: array:1 [▶]
          #connection: "mysql"
          #primaryKey: "id"
          #keyType: "int"
          +incrementing: true
          #with: []
          #withCount: []
          +preventsLazyLoading: false
          #perPage: 15
          +exists: true
          +wasRecentlyCreated: false
          #escapeWhenCastingToString: false
          #attributes: array:29 [▼
            "id" => 166
            "title" => "Individual Interview"
            "slug" => "individual-interview"
            "functionality_type" => "selection"
            "days_event_type_id" => 1
            "event_start" => "2021-11-03 09:00:00"
            "event_end" => "2021-11-03 19:00:00"
            "location" => "Online"
            "excerpt" => "Suit up and sit down with a recruiter to get in touch with the company on an individual level."
            "content" => "<div>Suit up and sit down with a recruiter to get in touch with the company on an individual level. The Individual Interview will last 30 or 50 minutes and is e ▶"
            "event_link" => "https://zoom.us/j/1234"
            "parent_event_id" => null
            "requires_motivation" => 1
            "requires_cv" => 1
            "requires_followup" => 1
            "requires_student_company_preference" => 0
            "company_id" => 34
            "force_in_calendar" => 0
            "spots" => 9
            "has_timeslots" => 1
            "has_algorithm_priority" => 1
            "timeslot_slots" => "["9:00-9:30","9:30-10:00","10:00-10:30","11:00-11:30","11:30-12:00","12:00-12:30","13:00-13:30","13:30-14:00","14:00-14:30"]"
            "timeslot_people" => 1
            "can_unsubscribe_late" => 1
            "company_can_see_cvs" => 1
            "sf_id" => null
            "deleted_at" => null
            "created_at" => "2021-01-04 10:33:38"
            "updated_at" => "2021-11-16 12:33:00"
          ]
          #original: array:29 [▶]
          #changes: []
          #classCastCache: []
          #dateFormat: null
          #appends: []
          #observables: []
          #relations: array:1 [▶]
          #touches: []
          +timestamps: true
          #hidden: []
          #visible: []
          #guarded: array:1 [▶]
          #forceDeleting: false
        }
      ]
      #escapeWhenCastingToString: false
    }
  ]
  #escapeWhenCastingToString: false
}

我想我可以使用 Laravel 中的 collect 函数来获取值。 (阅读这个非常similar question

但是当我尝试做的时候:$event_items = collect($events-&gt;items);

它给出了错误:Property [items] does not exist on this collection instance.

我在这里做错了什么?

【问题讨论】:

  • 试试 $events['03-11-2021'] 会返回想要的对象吗?
  • @MátyásGrőger 我想要所有事件的值。当我尝试 $events['03-11-2021'] 它提供了一个完整的事件。

标签: php arrays laravel multidimensional-array


【解决方案1】:

你不能直接使用物品, 你使用 $event_items = collect($events['03-11-2021']['0']->title);

【讨论】:

    【解决方案2】:

    你得到的这个结果基本上是模型集合的集合。集合实现数组访问,因此您可以使用 $collection['index'] 或使用 $collection-&gt;get('index')。如果你想要集合中的所有值,你可以调用$collection-&gt;all() 方法。我真的不明白你想从这里得到什么数据,或者为什么你首先有一个集合的集合,但 $collection-&gt;map()$collection-&gt;transform() 方法通常用于从集合中提取特定数据。

    我也相信$collection-&gt;items 是私有财产,所以如果您想要所有物品,它与$collection-&gt;all() 相同

    您可以查看Collections documentation 了解它们的工作原理。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-09
      相关资源
      最近更新 更多