How to get values from an array in Laravel - php

I have a 3 level category. I need to gain businesses on level 3 category.
I write this code:
$main_cat = Category::where(['slug' => $url])->first();
$lev_cat2 = Category::where(['parent_id' => $main_cat->id, 'status' => '1'])->get();
foreach ($lev_cat2 as $subCategory) {
$cat_ids_lv2[] = $subCategory->id . ',';
}
foreach ($lev_cat2 as $subCat) {
$lev_cat3[] = Category::where(['parent_id' => $subCat->id, 'status' => '1'])->pluck('id')->toArray();
}
dd($lev_cat3);
Then I got this array which is correct:
array:5 [▼
0 => array:3 [▼
0 => 145
1 => 146
2 => 147
]
1 => array:3 [▼
0 => 148
1 => 149
2 => 150
]
2 => array:3 [▼
0 => 151
1 => 152
2 => 153
]
3 => array:3 [▼
0 => 154
1 => 155
2 => 156
]
4 => []
]
now I dont know how can I get values like 145,146,147,148,149,... to pass theme to
Business::where(['category_id'=> [145,146,147,148,...]]->get();
of course dynamic.

You can use laravel collection helpers :
Business::whereIn('category_id', collect($lev_cat3)->flatten()->all())->get();

Since PHP 5.5.0 there is a built-in function array_column which does exactly this.
You can use it to Converting php array of arrays into single array then use it like this
$category_ids = array_column($lev_cat3);
Business::where(['category_id'=> $category_ids]]->get();

Related

Adding an array onto an array

i am attempting to make an array:
foreach ($cats as $cat) {
$catsList[$cat->id] = [$cat->info => $cat->info];
}
What i get is a resulting array that only contains the last $cat info.
array:2 [▼
10 => array:1 [▼
23 => 23
]
9 => array:1 [▼
11 => 11
]
]
What i expect to get is:
array:2 [▼
10 => array:1 [▼
23 => 23
15 => 15
12 => 12
]
9 => array:1 [▼
11 => 11
24 => 24
]
]
I guess the syntax is wrong when i'm trying to add a new member and it just overwrites the old one?
Can you try this:
foreach ($cats as $cat) {
$catsList[$cat->id][$cat->info] = $cat->info;
}

How can I remove duplicate array in between two arrays

I want to remove same array between 2 array
1 ) $getAlldate
array:8 [▼
0 => "2018-08-17"
1 => "2018-08-20"
2 => "2018-08-21"
3 => "2018-08-22"
4 => "2018-08-23"
5 => "2018-08-24"
6 => "2018-08-27"
7 => "2018-08-28"
]
2) $getHoliday
array:7 [▼
0 => "2019-1-1"
1 => "2019-3-6"
2 => "2019-2-28"
3 => "2019-5-2"
4 => "2019-4-25"
5 => "2018-8-27"
6 => "2018-8-28"
]
As you see in my array 2018-08-27,2018-08-28 are duplicate with $getholiday I try to use array_diff and It still didnt work
$a = array_diff($getAllDate, $getHoliday);
$b = array_unique($a);
How can I output like this $getalldate remove same value $getHoliday
array:8 [▼
0 => "2018-08-17"
1 => "2018-08-20"
2 => "2018-08-21"
3 => "2018-08-22"
4 => "2018-08-23"
5 => "2018-08-24"
]
The format of both the dates are diffrent.
Try:
PHP Code:
$getAlldate = [
"2018-08-17",
"2018-08-20",
"2018-08-21",
"2018-08-22",
"2018-08-23",
"2018-08-24",
"2018-08-27",
"2018-08-28"
];
$getHoliday=[
"2019-1-1",
"2019-3-6",
"2019-2-28",
"2019-5-2",
"2019-4-25",
"2018-8-27",
"2018-8-28"
];
$formattedHoliday = array_map(function ($holiday) {
$alldateFormat = 'Y-m-d';
$HolidayFormat = 'Y-n-d';
$date = DateTime::createFromFormat($HolidayFormat, $holiday);
return $date->format($alldateFormat);
}, $getHoliday);
var_dump(array_diff($getAlldate, $formattedHoliday));
Output: https://3v4l.org/rCLCk
Refer:
DateTime::createFromFormat: http://php.net/manual/en/datetime.createfromformat.php
DateTime::format: http://php.net/manual/en/datetime.format.php

Calculate Age for each user from array of date using Laravel

i'm new to laravel i want to know if there are methode to calculate age from array so my content looks like this:
Collection {#231 ▼
#items: array:6 [▼
0 => "1928-11-18"
1 => "1938-06-15"
2 => "1939-03-30"
3 => "1941-11-08"
4 => "1940-04-29"
5 => "1987-06-24"
]
}
How to properly transform the array so that it contains only age like this
Collection {#231 ▼
#items: array:6 [▼
0 => 90
1 => 80
2 => 79
3 => 77
4 => 87
5 => 31
]
}
You can also use map method on collection
$collection = collect([0 => "1928-11-18",
1 => "1938-06-15",
2 => "1939-03-30",
3 => "1941-11-08",
4 => "1940-04-29",
5 => "1987-06-24"]);
$age = $collection->map(function ($item, $key) {
return Carbon::parse($item)->diff(Carbon::now())->format('%y');
});
return $age->all();
This will give you,
[
"89",
"79",
"79",
"76",
"78",
"30"
]
You can iterate trough a Collection with foreach() then convert to Age using Carbon ->age:
foreach ($collection as $key => $value) {
$collection[$key] = Carbon::parse($collection[$key])->age;
}
This will give you a Collection as what you do is editing the Collection, instead of creating a new array.
Use Carbon's ->age or diffInYears and collection transform (or map):
$dates->transform(function ($date) {
return \Carbon\Carbon::parse($date)->age;
});
Note: someone born on 1928-11-18 is 89 years old, not 90.

Laravel collection by month

I've got a collection and I need the total users by month.
So I've done this:
array_replace(array_fill_keys(range(0, 11), 0), $users->groupBy('created_at.month')->toArray())
It's sort of working because I get this:
array:12 [▼
0 => 0
1 => 0
2 => 0
3 => 0
4 => 0
5 => 0
6 => array:1 [▶]
7 => array:2 [▶]
8 => 0
9 => 0
10 => 0
11 => 0
]
The problem I face now is that I don't need the 2 arrays at position 6 and 7 but I need the counts.
So I was thinking of something like:
$users->groupBy('created_at.month')->count()->toArray();
But, that's obviously not working. Any ideas ?
try using map method
$collection->map(function ($item, $key) {
return count($item);
});

Concatenate strings from 2 array based on key

so i have a bit difficulty in combining arrays in php. So let say i have these 2 array
array:4 [▼
20 => "University"
21 => "Polic Station"
22 => "Ambulance"
1 => "Zoo"
]
array:4 [▼
20 => "abc"
21 => "def"
22 => "ghi"
1 => "jkl"
]
How do i actually combine this to this
array:4 [▼
20 => "abc University"
21 => "def Polic Station"
22 => "ghi Ambulance"
1 => "jkl Zoo"
]
Here's the result:
$arr = array(
20=>'University',
21=>'Polic Station',
22=>'Ambulance',
1=>'Zoo');
$arr2= array(
20=>'abc',
21=>'def',
22=>'ghi',
1=>'jkl');
$arr_out = array();
foreach($arr as $key=>$el) {
$arr_out[$key] = $arr2[$key] ." ".$el;
}
var_dump($arr_out);
Obviously, you'll need to keep in mind to check whether the key exists in the second array so you do not get an error when accessing the value.

Categories