John Davidson

php - Get particular values multidimensional array Laravel

0 comments
Message:


I want to get the values of some fields form the #attributes field (like "title" and "location") so I can fill a calendar.


dd($events); Outputs:


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
}

I think I can get the values using the collect function form Laravel. (read this on this very similar question)


But when I try do do: $event_items = collect($events->items);


It gives the error: Property [items] does not exist on this collection instance.


What am I doing wrong here?



My Answer:
multidimensional array in Laravel. You can achieve this by using the array_pluck() function provided by Laravel.

Here's an example of how you can get particular values from a multidimensional array in Laravel:

php
$data = [
['id' => 1, 'name' => 'John', 'age' => 25],
['id' => 2, 'name' => 'Jane', 'age' => 30],
['id' => 3, 'name' => 'Alice', 'age' => 28],
];

$names = array_pluck($data, 'name');
$ages = array_pluck($data, 'age');

dd($names, $ages);


In this example, we have a multidimensional array `$data` containing information about different people. We use the `array_pluck()` function to extract the values of the 'name' and 'age' fields from the array. The resulting `$names` and `$ages` arrays will contain the values of the 'name' and 'age' fields respectively.

You can then use these extracted values as needed in your Laravel application.

Rate this post

4 of 5 based on 7208 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us