remove elements in multidimensional array based on two values - php

I have in my example 2 arrays of court bookings for a particular date and time on 5 courts, with date, court number, member and type.
I want to remove items from the second array where there is a matching date/court in the first array.
Here is one timeslot. The first array shows the 2 bookings in this timeslot.
Array
(
[0] => Array
(
[0] => Jul 6, 2014 7:00 AM
[1] => 1
[2] => Annie
[3] => Regular
)
[1] => Array
(
[0] => Jul 6, 2014 7:00 AM
[1] => 3
[2] => Meredith
[3] => Regular
)
)
The second array is all the timeslots available on that date/time.
Array
(
[2] => Array
(
[0] => Jul 6, 2014 7:00 AM
[1] => 1
[2] =>
[3] => UNBOOKED
)
[3] => Array
(
[0] => Jul 6, 2014 7:00 AM
[1] => 2
[2] =>
[3] => UNBOOKED
)
[4] => Array
(
[0] => Jul 6, 2014 7:00 AM
[1] => 3
[2] =>
[3] => UNBOOKED
)
[5] => Array
(
[0] => Jul 6, 2014 7:00 AM
[1] => 4
[2] =>
[3] => UNBOOKED
)
[6] => Array
(
[0] => Jul 6, 2014 7:00 AM
[1] => 5
[2] =>
[3] => UNBOOKED
)
)
What I want to do is remove items in the second array which have a match on elements [0] and [1]. Every solution I've tried just searches arrays for strings, not another array.
The result would ideally look like this (the outer numeric keys are not important to me).
Array
(
[0] => Array
(
[0] => Jul 6, 2014 7:00 AM
[1] => 1
[2] => Annie
[3] => Regular
)
[1] => Array
(
[0] => Jul 6, 2014 7:00 AM
[1] => 3
[2] => Meredith
[3] => Regular
)
[3] => Array
(
[0] => Jul 6, 2014 7:00 AM
[1] => 2
[2] =>
[3] => UNBOOKED
)
[5] => Array
(
[0] => Jul 6, 2014 7:00 AM
[1] => 4
[2] =>
[3] => UNBOOKED
)
[6] => Array
(
[0] => Jul 6, 2014 7:00 AM
[1] => 5
[2] =>
[3] => UNBOOKED
)
)

This is how you would use array filter to remove entries in the list of available times that exist in the list of booked times.
You can see this in action here: http://codepad.viper-7.com/VVbnys
$regular = Array(
0 => Array (
0 => 'Jul 6, 2014 7:00 AM',
1 => 1,
2 => 'Annie',
3 => 'Regular',
),
1 => Array (
0 => 'Jul 6, 2014 7:00 AM',
1 => 3,
2 => 'Meredith',
3 => 'Regular',
)
);
$unbooked = Array (
2 => Array
(
0 => 'Jul 6, 2014 7:00 AM',
1 => 1,
2 => '',
3 => 'UNBOOKED',
),
3 => Array
(
0 => 'Jul 6, 2014 7:00 AM',
1 => 2,
2 => '',
3 => 'UNBOOKED',
),
4 => Array
(
0 => 'Jul 6, 2014 7:00 AM',
1 => 3,
2 => '',
3 => 'UNBOOKED',
),
5 => Array
(
0 => 'Jul 6, 2014 7:00 AM',
1 => 4,
2 => '',
3 => 'UNBOOKED',
),
6 => Array
(
0 => 'Jul 6, 2014 7:00 AM',
1 => 5,
2 => '',
3 => 'UNBOOKED',
),
);
//filter the unbooked array
$unbooked = array_filter($unbooked, function($availSlot) use ($regular){
//loop over each taken slot
foreach($regular as $takenSlot){
//if keys 0 and 1 are the same in both taken and available...
if($availSlot[0] == $takenSlot[0] && $availSlot[1] == $takenSlot[1]){
//return false to remove this element from the array
return false;
}
}
//if we get here, no matches were found.
//return true so the element stays in the array
return true;
});
Ideally though, the format would be changed to a multidimensional array that allows you to nest stuff under the timeslots and numbers that are available without having to loop over every entry looking for one that matches.

Related

Way to compare array key & replace value if found (PHP)

Hello i am trying to make an php application, but i am having some trouble with arrays
Actually,
I m trying to compare array key & if found replace its value & merge whole array into one.
For example
Array ONE-
Array
(
[0] => Array
(
[label] => July 20 2020
[y] => 3
)
[1] => Array
(
[label] => July 18 2020
[y] => 1
)
)
Array TWO-
Array
(
[0] => Array
(
[label] => July 18 2020
[y] => 0
)
[1] => Array
(
[label] => July 19 2020
[y] => 0
)
[2] => Array
(
[label] => July 20 2020
[y] => 0
)
[3] => Array
(
[label] => July 21 2020
[y] => 0
)
)
To
desired output array
Array
(
[0] => Array
(
[label] => July 18 2020
[y] => 1
)
[1] => Array
(
[label] => July 19 2020
[y] => 0
)
[2] => Array
(
[label] => July 20 2020
[y] => 3
)
[3] => Array
(
[label] => July 21 2020
[y] => 0
)
)
I have tried
array_merge($arrayone, $arraytwo);
but it doesn't work.
Is it possible to get the desired output ?
This is how array looks like - https://imgur.com/a/HUQUmZz
Please share your thoughts on this.
Thankyou
With your arrays, I do not think there is a smart way to do it, but this should work :
<?php
$datesArray = [
[
'label' => 'July 18 2020',
'y' => 0,
],
[
'label' => 'July 19 2020',
'y' => 0,
],
[
'label' => 'July 20 2020',
'y' => 0,
],
[
'label' => 'July 21 2020',
'y' => 0,
],
];
$valuesArray = [
[
'label' => 'July 20 2020',
'y' => 3,
],
[
'label' => 'July 18 2020',
'y' => 1,
],
];
foreach($datesArray as &$dateArray) {
foreach($valuesArray as $valueArray) {
// use ?? if php version allows it, else do isset($valueArray['label']) ? $valueArray['label'] : null;
$currentLabel = $valueArray['label'] ?? null;
if ($currentLabel === $dateArray['label']) {
$dateArray['y'] = $valueArray['y'] ?? 0;
}
}
}
print_r($datesArray);
return $datesArray;
Result :
Array
(
[0] => Array
(
[label] => July 18 2020
[y] => 1
)
[1] => Array
(
[label] => July 19 2020
[y] => 0
)
[2] => Array
(
[label] => July 20 2020
[y] => 3
)
[3] => Array
(
[label] => July 21 2020
[y] => 0
)
)
This will take quadratic time, if you need something faster you will have to change the indices of your arrays to make a faster lookup.
snippet

Creating multi-dimensional array from multiple distinct arrays

I suspect this question has been answered before but i have dug and dug this great forum for an answer in vain.....
I have 3 arrays that looks like this:
Array
(
[1] => 19
[2] => 2
[3] => 2018
)
Array
(
[1] => 19
[2] => 1
[3] => 2017
)
Array
(
[1] => 18
[2] => 2
[3] => 2016
)
I would like to convert this 3 arrays into a multidimensional array to look something like this:
$mynewArray = Array(
[0] =>array(
[1] => 19
[2] => 2
[3] => 2018
)
[1] =>array(
[1] => 19
[2] => 1
[3] => 2017
)
[2] => array(
[1] => 18
[2] => 2
[3] => 2016
)
)
How do i achieve this in Php?
Demo Link.
You just need to add it in parent array as below,
$arr1 = [1 => 19, 2 => 2, 3 => 2018];
$arr2 = [1 => 19, 2 => 1, 3 => 2017];
$arr3 = [1 => 18, 2 => 2, 3 => 2016];
$mynewArray = [$arr1,$arr2,$arr3];
print_r($mynewArray);
Output
Array
(
[0] => Array
(
[1] => 19
[2] => 2
[3] => 2018
)
[1] => Array
(
[1] => 19
[2] => 1
[3] => 2017
)
[2] => Array
(
[1] => 18
[2] => 2
[3] => 2016
)
)
Also you can append your child arrays to the parent by
$array1 = array("1"=>"1","2"=>"2","3"=>"3");
$array2 = array("1"=>"1","2"=>"2","3"=>"3");
$newarray = array($array1,$array2);

arrange arrays inside arrays on basis of a value

i have an array of arrays like below the length of the arrays will be unknown i want to sort the arrays in ascending order on basis of the third element in every array.
Array
(
[0] => Array
(
[0] => 30
[1] => AbdulA
[2] => TEST White Truck 7 am 9:30 am 12 pm 2:30 pm
[3] => 8
[4] => 12
)
[1] => Array
(
[0] => 31
[1] => AbdulAnn
[2] => TEST White Truck 7 am 9:30 am 12 pm 2:30 pm
[3] => 2
[4] => 8
)
[2] => Array
(
[0] => 32
[1] => pacha
[2] => TEST RED TRUCK
[3] => 0
[4] => 1
)
)
You can use usort for this:
function cmp($a, $b) {
return $a[3] - $b[3];
}
usort($array, "cmp");

Php, sum and average values in array

I have an array wherein I need to return the average of the "price" & "perUnitVolume" and the sum of the "qty" WHERE the Year & Month match. For example in this array, 2 elements in the array have Year = 2014 & Month = 11, these elements need to be summed and averaged. This is a simplified output of my array (actual array has > 1000 elements in the array, many variations of Year and Month), just don't know how to get the results that I need using php.
Array
(
[0] => Array
(
[asin] => XXXXXX
[price] => 19.99
[qtyTotal] => 22
[perUnitVolume] => 0.22
[Month] => 2014
[Year] => 11
[Day] => 14
)
[1] => Array
(
[asin] => YYYYYYY
[price] => 24.99
[qtyTotal] => 53
[perUnitVolume] => 0.54
[Month] => 2014
[Year] => 11
[Day] => 15
)
[2] => Array
(
[asin] => XXXXXX
[price] => 19.99
[qtyTotal] => 13
[perUnitVolume] => 0.99
[Month] => 2015
[Year] => 08
[Day] => 16
)
)
Basically in the end I need to end up with this:
Array
(
[0] => Array
(
[asin] => XXXXXX
[price] => 22.49 (average of 19.99 + 24.99 / 2)
[qtyTotal] => 75 (sum of 22 + 53)
[perUnitVolume] => 0.38 (average of 0.22 + 0.54 / 2)
[Month] => 2014
[Year] => 11
)
[1] => Array
(
[asin] => YYYYYYY
[price] => 19.99
[qtyTotal] => 53
[perUnitVolume] => 0.54
[Month] => 2015
[Year] => 08
)
)

How to create multidimensional array with key value pair php

I have two arrays like this :
Array 1 is having x-axis values in array y-axis values in array
x-array: ["04 Feb","05 Feb","06 Feb","07 Feb","08 Feb","09 Feb","10 Feb"]
y-array: [3.27,3.34,3.27,3.2,3.28,3.17,3.15]
Array2 is having x-axis values in array y-axis values in array
x-array2: ["11 Feb", "12 Feb"]
y-array2: [3.19, 3.36]
How to create multidimensional array with key value pair.
Like below in php
Array
(
[0] => Array
(
[x-axis] => Array
(
[0] => 04 Feb
[1] => 05 Feb
[2] => 06 Feb
[3] => 07 Feb
[4] => 08 Feb
[5] => 09 Feb
[6] => 10 Feb
)
[y-axis] => Array
(
[0] => 3.27
[1] => 3.34
[2] => 3.27
[3] => 3.2
[4] => 3.28
[5] => 3.17
[6] => 3.15
)
)
[1] => Array
(
[x-axis] => Array
(
[0] => 11 Feb
[1] => 12 Feb
)
[y-axis] => Array
(
[0] => 3.19
[1] => 3.36
)
)
)
$new_array = array(array("x-axis"=>$x-axis-array,
"y-axis"=>$y-axis-array
),
array("x-axis"=>$x-axis-array2,
"y-axis"=>$y-axis-array2
)
);
echo "<pre>";
print_r($new_array);
$array = array(
0 => array(
"x-axis" => $x_array,
"y-axis" => $y_array),
1 => array(
"x-axis" => $x_array2,
"y-axis" => $y_array2)
) ;
var_dump($array) ;

Categories