Reputation: 361
I am new to laravel and sometimes when I follow tutorials I see different forms of assigning the arrays' keys, can anyone explain the difference between them.
$post [
'title' => 'First title',
'body' => 'First body'
];
$post [
'title', 'First title',
'body', 'First body'
];
Upvotes: 0
Views: 80
Reputation: 3571
It is not a Laravel thing, but has to do with PHP.
The first way is called an associative array. You should use this if there is a relation between the key before the =>
and the value after it. You could get the title by writing $post['title']
.
The second is an array where there are no relationships between the values. It's just a collection of the same type of thing. You could also write this like this:
0 = 'title',
1 = 'First title',
2 = 'body',
3 = 'First body'
If you had an array called posts
, it would be most likely to look like a combination of the two. So a numeric array for all posts and a associative array for each posts.
$posts = [
[
'title' => 'First title',
'body' => 'First body'
],
[
'title' => 'Second title',
'body' => 'Second body'
],
];
Do some research into (PHP) arrays and you'll figure it out.
Upvotes: 4