unset array element if length is less - php

I've following array:
Array
(
[1] => Array
(
[team1_id] => 2
[agegroup_id] => 18
[team2_id] => 3
[team_ground] => Adeilade
[matchdate] => 2016-04-01
[matchtime] => 9:00 AM
)
[2] => Array
(
[team1_id] => 3
[agegroup_id] => 18
[team2_id] => 2
[team_ground] => Adeilade
[matchdate] => 2016-04-13
[matchtime] => 10:00 AM
)
[3] => Array
(
[team1_id] => 2
[agegroup_id] => 18
[team2_id] => 3
[team_ground] => Adeilade
[matchdate] => 2016-04-20
[matchtime] => 2:00 PM
)
[4] => Array
(
[team1_id] => 3
[agegroup_id] => 18
[team2_id] => 2
[team_ground] => Adeilade
[matchdate] => 04/07/2016
[matchtime] => 5:00 PM
)
[6] => Array
(
[team1_id] => 9
[agegroup_id] => 36
[team2_id] => 4
[team_ground] => Motera Stadium
[matchdate] => 2016-04-13
[matchtime] => 9:00 AM
)
[7] => Array
(
[team1_id] => 4
[agegroup_id] => 36
[team2_id] => 9
[team_ground] => Motera Stadium
[matchdate] => 2016-04-13
[matchtime] => 5:00 PM
)
[9] => Array
(
[team1_id] => 1
[agegroup_id] => 37
[team2_id] => 8
[team_ground] => Eden Garden
[matchdate] => 2016-04-18
[matchtime] => 7:00 PM
)
[10] => Array
(
[team1_id] => 8
[agegroup_id] => 37
[team2_id] => 1
[team_ground] => Eden Garden
[matchdate] => 2016-04-25
[matchtime] => 8:00 PM
)
[11] => Array
(
[team1_id] => 1
[agegroup_id] => 37
[team2_id] => 8
[team_ground] => Eden Garden
[matchdate] => 04/26/2016
[matchtime] => 8:00 PM
)
[0] => Array
(
[agegroup_id] => 18
)
[5] => Array
(
[agegroup_id] => 36
)
[8] => Array
(
[agegroup_id] => 37
)
)
If array is having only one element then I've to unset it. In this case I want to unset array index 0,5,8 in PHP. How to unset the array index when it is having less elements?

You can simply use array_filter over here as
$result = array_filter($your_array,function($v){ return count($v) > 1;});
print_r($result);

You can also use a simple foreach:
foreach ($array as $key => $value)
{
if(sizeOf($array[$key]) < 2)
unset($array[$key]);
}
Here is a working DEMO

Related

mysql query child parent relation

I have a query
SELECT bs.i_description,col.Column_name,bs.Tm_id,ad.UserName as name,
a.fk_sprint_id as spid,b.fk_back_id,b.u_pos_is,b.s_id,b.color,b._left,b._top,
b.wiptime as wiptime,b.dodtime as dodtime,b.Dep_status
FROM backToSprint a
JOIN backToSprint b ON a.`s_id` = b.`fk_f_id`
join backlog bs on bs.b_id=b.fk_back_id
left join admin ad on bs.Tm_id=ad.adminID
left join admin_column col on col.column_id=b.u_pos_is
where a.fk_sprint_id=3 and a.teamid=1
ORDER BY a.`fk_f_id`
Where I got output like
Array
(
[0] => Array
(
[i_description] => Story 1
[Column_name] => Backlogs
[Tm_id] => 0
[name] =>
[spid] => 3
[fk_back_id] => 408
[u_pos_is] => 1
[s_id] => 5
[color] => 2
[_left] => 18
[_top] => -9
[wiptime] =>
[dodtime] =>
[Dep_status] => 0
)
[1] => Array
(
[i_description] => Story 2
[Column_name] => Backlogs
[Tm_id] => 0
[name] =>
[spid] => 3
[fk_back_id] => 409
[u_pos_is] => 1
[s_id] => 6
[color] => 3
[_left] => 18
[_top] => -9
[wiptime] =>
[dodtime] =>
[Dep_status] => 0
)
)
Here Column name is same so I expect output like
Array
(
['c'] => Array
(
[Column_name] => Backlogs
)
[0] => Array
(
[i_description] => Story 1
[Column_name] => Backlogs
[Tm_id] => 0
[name] =>
[spid] => 3
[fk_back_id] => 408
[u_pos_is] => 1
[s_id] => 5
[color] => 2
[_left] => 18
[_top] => -9
[wiptime] =>
[dodtime] =>
[Dep_status] => 0
)
[1] => Array
(
[i_description] => Story 2
[Column_name] => Backlogs
[Tm_id] => 0
[name] =>
[spid] => 3
[fk_back_id] => 409
[u_pos_is] => 1
[s_id] => 6
[color] => 3
[_left] => 18
[_top] => -9
[wiptime] =>
[dodtime] =>
[Dep_status] => 0
)
)
Then i tried
$this->view->getstories['c'] = ['Column_name' => array_unique(array_column( $this->view->getstories, 'Column_name'))];
which didn't get actual output.i got output like
Array
(
[0] => Array
(
[i_description] => Story 1
[Column_name] => Backlogs
[Tm_id] => 0
[name] =>
[spid] => 3
[fk_back_id] => 408
[u_pos_is] => 1
[s_id] => 5
[color] => 2
[_left] => 18
[_top] => -9
[wiptime] =>
[dodtime] =>
[Dep_status] => 0
)
[1] => Array
(
[i_description] => Story 2
[Column_name] => WIP
[Tm_id] => 0
[name] =>
[spid] => 3
[fk_back_id] => 409
[u_pos_is] => 2
[s_id] => 6
[color] => 3
[_left] => 18
[_top] => -9
[wiptime] =>
[dodtime] =>
[Dep_status] => 0
)
[c] => Array
(
[Column_name] => Array
(
[0] => Backlogs
[1] => WIP
)
)
)
(Here i have another column name WIP)
But i want a nested array like
Array
(
[c1] => Array
(
[Column_name] => Array
(
[0] => Backlogs
)
[0] => Array
(
[i_description] => Story 1
[Column_name] => Backlogs
[Tm_id] => 0
[name] =>
[spid] => 3
[fk_back_id] => 408
[u_pos_is] => 1
[s_id] => 5
[color] => 2
[_left] => 18
[_top] => -9
[wiptime] =>
[dodtime] =>
[Dep_status] => 0
)
[c2] => Array
(
[Column_name] => Array
(
[0] => WIP
)
[0] => Array
(
[i_description] => Story 2
[Column_name] => WIP
[Tm_id] => 0
[name] =>
[spid] => 3
[fk_back_id] => 409
[u_pos_is] => 2
[s_id] => 6
[color] => 3
[_left] => 18
[_top] => -9
[wiptime] =>
[dodtime] =>
[Dep_status] => 0
)
)
)
Please help me to solve this issue
Any help would be appreciated.
What you want is nested arrays. Do it while fetching the rows from the query, using $row['Column_name'] as the key of the main array, and pushing each row onto that nested array.
$results = [];
while ($row = $stmt->fetch()) {
$results[$row['Column_name']][] = $row;
}
This will create a result like:
Array
(
[Backlogs] => Array
(
[0] => Array
(
[i_description] => Story 1
[Column_name] => Backlogs
[Tm_id] => 0
[name] =>
[spid] => 3
[fk_back_id] => 408
[u_pos_is] => 1
[s_id] => 5
[color] => 2
[_left] => 18
[_top] => -9
[wiptime] =>
[dodtime] =>
[Dep_status] => 0
)
)
[WIP] => Array
(
[0] => Array
(
[i_description] => Story 2
[Column_name] => WIP
[Tm_id] => 0
[name] =>
[spid] => 3
[fk_back_id] => 409
[u_pos_is] => 2
[s_id] => 6
[color] => 3
[_left] => 18
[_top] => -9
[wiptime] =>
[dodtime] =>
[Dep_status] => 0
)
)
)
)
There's no need for the c1 and c2 keys, just use the Column_name values as the keys of the main array.

How to delete keys from multi-dimension array dynamically

I have to filter this array to see only future items.
How do I unset items from which the timeEnd has expired?
i.e when I call this array at 17:00 there's only array[2] left.
Array
(
[0] => Array
(
[id] => 3034
[date] => 28-09-2016
[timeStart] => 08:30
[timeEnd] => 09:30
[description] => User_A
[locationId] => 1
[roomId] => 8
[relationId] => 104
)
[1] => Array
(
[id] => 2524
[date] => 28-09-2016
[timeStart] => 08:30
[timeEnd] => 12:00
[description] => User_B
[locationId] => 1
[roomId] => 5
[relationId] => 86
)
[2] => Array
(
[id] => 2533
[date] => 28-09-2016
[timeStart] => 09:00
[timeEnd] => 18:00
[description] => User_C
[locationId] => 1
[roomId] => 4
[relationId] => 31
)
)
foreach ($reservations as $key=>$reservation) {
$expireDate = $reservation['date'].' '.$reservation['timeEnd'];
if (strtotime($expireDate) <= strtotime('now')){
unset($reservations[$key]);
}
}

How to generate array from an array

I have an array but I want to generate other array from that one..
Array
(
[0] => Array
(
[supplier] => Billy
[total_bookings] => 5
[year] => 2016
[month] => 6
[user_id] => 4
[sales_revenue] => 1180
[net_revenue] => 1180
)
[1] => Array
(
[supplier] => XYZ1
[total_bookings] => 3
[year] => 2016
[month] => 6
[user_id] => 2
[sales_revenue] => 642
[net_revenue] => 642
)
[2] => Array
(
[supplier] => Billy
[total_bookings] => 1
[year] => 2016
[month] => 3
[user_id] => 4
[sales_revenue] => 30
[net_revenue] => 30
)
[3] => Array
(
[supplier] => Billy
[total_bookings] => 1
[year] => 2015
[month] => 10
[user_id] => 4
[sales_revenue] => 30
[net_revenue] => 30
)
)
to new array :
Array
(
[2016] => Array(
[6] => Array
(
[0] => Array(
[supplier] => Billy
[total_bookings] => 5
[user_id] => 4
[sales_revenue] => 1180
[net_revenue] => 1180
)
[1] => Array
(
[supplier] => XYZ1
[total_bookings] => 3
[user_id] => 2
[sales_revenue] => 642
[net_revenue] => 642
)
)
[3] => Array
(
[0] => Array
(
[supplier] => Billy
[total_bookings] => 1
[year] => 2016
[month] => 3
[user_id] => 4
[sales_revenue] => 30
[net_revenue] => 30
)
)
)
[2015] => Array(
[10] => Array
(
[supplier] => Billy
[total_bookings] => 1
[user_id] => 4
[sales_revenue] => 30
[net_revenue] => 30
)
)
)
The solution using array_fill_keys, array_column(available since PHP 5.5), array_walk and array_diff_key functions:
// supposing $arr is your initial array
$years = array_fill_keys(array_column($arr, 'year'), []);
array_walk($arr, function($v) use(&$years){
if (key_exists($v['year'], $years)) {
$years[$v['year']][$v['month']][] = array_diff_key($v, ['year'=>0, 'month'=>0]);
}
});
print_r($years);
Try this one and let me know:
$new_arr = array();
foreach($arr as $val){
$temp = array('supplier' => $val['supplier'], 'total_bookings' => $val['total_bookings'], 'user_id' => $val['user_id'], 'sales_revenue' => $val['sales_revenue'], 'net_revenue' => $val['net_revenue']);
array_push($new_arr[$val['year']][$val['month']], $temp);
}
print_r($new_arr);
Try this one:-
$res = [];
foreach($array as $record){
$year = $record['year'];
$month = $record['month'];
unset($record['year'],$record['month']);
$res[$year][$month][] = $record;
}
echo '<pre>'; print_r($res);

How to combine specific value in two array to one array with exception?

I have following two different array
First array:
Array
(
[0] => stdClass Object
(
[course_id] => 21
[session_id] => 17
[course_name] => Session 1 Course 1
[course_description] => Session 1 Course 1
[course_days] => 2
[current_rate] => 1000
[old_rate] =>
[primary_tutor_id] => 18
[secondary_tutor_id] => 25
[additional_time] =>
[location] =>
[course_active] => 1
)
[1] => stdClass Object
(
[course_id] => 22
[session_id] => 17
[course_name] => Session 1 Course 2
[course_description] => Session 1 Course 2
[course_days] => 3
[current_rate] => 1000
[old_rate] =>
[primary_tutor_id] => 24
[secondary_tutor_id] => 25
[additional_time] =>
[location] =>
[course_active] => 1
)
[2] => stdClass Object
(
[course_id] => 23
[session_id] => 17
[course_name] => Session 1 Course 3
[course_description] => Session 1 Course 3
[course_days] => 5
[current_rate] => 2000
[old_rate] =>
[primary_tutor_id] => 26
[secondary_tutor_id] => 27
[additional_time] =>
[location] =>
[course_active] => 1
)
[3] => stdClass Object
(
[course_id] => 26
[session_id] => 19
[course_name] => Session 3 Course 2
[course_description] => Session 3 Course 2
[course_days] => 2
[current_rate] => 400
[old_rate] =>
[primary_tutor_id] => 29
[secondary_tutor_id] => 29
[additional_time] =>
[location] =>
[course_active] => 1
)
[4] => stdClass Object
(
[course_id] => 27
[session_id] => 19
[course_name] => Session 3 Course 3
[course_description] => Session 3 Course 3
[course_days] => 1
[current_rate] => 200
[old_rate] =>
[primary_tutor_id] => 26
[secondary_tutor_id] =>
[additional_time] =>
[location] =>
[course_active] => 1
)
)
Second Array:
Array
(
[0] => stdClass Object
(
[discount_id] => 104
[session_id] => 17
[no_course] => 3
[discount] => 20
)
[1] => stdClass Object
(
[discount_id] => 106
[session_id] => 19
[no_course] => 2
[discount] => 20
)
)
I am trying to combine second array all key and value into first array like following example
Array
(
[0] => stdClass Object
(
[course_id] => 21
[session_id] => 17
[course_name] => Session 1 Course 1
[course_description] => Session 1 Course 1
[course_days] => 2
[current_rate] => 1000
[old_rate] =>
[primary_tutor_id] => 18
[secondary_tutor_id] => 25
[additional_time] =>
[location] =>
[course_active] => 1
[discount_id] => 104
[session_id] => 17
[no_course] => 3
[discount] => 20
)
)
I have have tried following code and many other code but i cant get perfect solution. In this two array there is one exception if both [session_id] is same than need array merging other wise as it is array with out merging :
$main=array();
foreach ($courses as $key => $val) {
foreach ($session_discount as $se) {
if ($se->session_id == $val->session_id)
$main[$key] = $val;
array_push($main[$key], $session_discount[0]);
}
}
Need help..! Thanks in Advance.

Sorting Multi-Dimensional Array Not Working

I have an array variable $data and I am trying to sorting it alphabetically. I am going through a foreach loop and trying to sort of the key.
The array in the middle is not being sorted to match the other arrays. Yelp should be the last one, instead DealerRater is showing last.
I am trying this:
foreach ($data as $key=>$value) {
ksort($key);
}
My $data Array:
Array
(
[1] => Array
(
[Cars.com] => Array
(
[rooftop_id] => 1
[rooftop_name] => Norm Reeves Honda - Cerritos
[name] => Cars.com
[review_site_id] => 30
[review_count] => 289
[review_average] => 4.80
)
[Dealer Rater] => Array
(
[rooftop_id] => 1
[rooftop_name] => Norm Reeves Honda - Cerritos
[name] => Dealer Rater
[review_site_id] => 10
[review_count] => 1231
[review_average] => 4.90
)
[Google+ Local] => Array
(
[rooftop_id] => 1
[rooftop_name] => Norm Reeves Honda - Cerritos
[name] => Google+ Local
[review_site_id] => 31
[review_count] => 556
[review_average] => 4.80
)
[Yelp] => Array
(
[rooftop_id] => 1
[rooftop_name] => Norm Reeves Honda - Cerritos
[name] => Yelp
[review_site_id] => 29
[review_count] => 423
[review_average] => 3.50
)
)
[45] => Array
(
[Cars.com] => Array
(
[rooftop_id] => 45
[rooftop_name] => Leith Volkswagen of Raleigh
[name] => Cars.com
[review_site_id] => 30
[review_count] => 95
[review_average] => 4.90
)
[Google+ Local] => Array
(
[rooftop_id] => 45
[rooftop_name] => Leith Volkswagen of Raleigh
[name] => Google+ Local
[review_site_id] => 31
[review_count] => 21
[review_average] => 4.80
)
[Yelp] => Array
(
[rooftop_id] => 45
[rooftop_name] => Leith Volkswagen of Raleigh
[name] => Yelp
[review_site_id] => 29
[review_count] => 3
[review_average] => 1.50
)
[Dealer Rater] => Array
(
[rooftop_id] => 45
[rooftop_name] => Leith Volkswagen of Raleigh
[name] => Dealer Rater
[review_site_id] => 10
[review_count] => 0
[review_average] => 0
)
)
[56] => Array
(
[Cars.com] => Array
(
[rooftop_id] => 56
[rooftop_name] => Wilde Jaguar Of Sarasota
[name] => Cars.com
[review_site_id] => 30
[review_count] => 34
[review_average] => 4.70
)
[Dealer Rater] => Array
(
[rooftop_id] => 56
[rooftop_name] => Wilde Jaguar Of Sarasota
[name] => Dealer Rater
[review_site_id] => 10
[review_count] => 271
[review_average] => 4.90
)
[Google+ Local] => Array
(
[rooftop_id] => 56
[rooftop_name] => Wilde Jaguar Of Sarasota
[name] => Google+ Local
[review_site_id] => 31
[review_count] => 31
[review_average] => 4.70
)
[Yelp] => Array
(
[rooftop_id] => 56
[rooftop_name] => Wilde Jaguar Of Sarasota
[name] => Yelp
[review_site_id] => 29
[review_count] => 1
[review_average] => 1.00
)
)
)
You're sorting the wrong value. Do this:
foreach ($data as $key=>$value) {
ksort($value);
}
$key is the index of the main array. $value is the array element you want to sort. It'd make more sense if you named your variables more logically, like this:
foreach ($data as $index=>$element) {
ksort($element);
}
Or this for short:
foreach ($data as $element) {
ksort($element);
}

Categories