Merge 2 arrays having same number of items - php [duplicate] - php

This question already has answers here:
Merge row data from multiple arrays
(6 answers)
Closed 4 months ago.
The number of items in both arrays $newResponse & $key will always be same.For each item of the $newResponse, i wanted to add items of $key to $newResponse in the same order they exists in $key array as mentioned in the Expected result.How can i achieve that?
dump($newResponse); is an array like below,which am getting as the result of a foreach loop.
array:4 [
0 => array:6 [
"courseId" => 18
"courseDisplayName" => "qqq"
]
1 => array:6 [
"courseId" => 1
"courseDisplayName" => "ips"
]
2 => array:6 [
"courseId" => 18
"courseDisplayName" => "qqq"
]
3 => array:6 [
"courseId" => 1
"courseDisplayName" => "ips"
]
]
dump($key); is an array like below, which is the result of another foreach loop.
array:4[
0=>[
"totalPoints" => 2
"percent" => 1.0
"id" => 2
]
1=> [
"totalPoints" => 10
"percent" => 2
"id" => 3
]
2=> [
"totalPoints" => 4
"percent" => 0.0
"id" => 6
]
3=> [
"totalPoints" => 4
"percent" => 0.0
"id" => 5
]
]
Expected result:
[
[
"courseId" => 18
"courseDisplayName" => "qqq"
"totalPoints" => 2
"percent" => 1.0
"id" => 2
]
[
"courseId" => 1
"courseDisplayName" => "ips"
"totalPoints" => 10
"percent" => 2
"id" => 3
]
[
"courseId" => 18
"courseDisplayName" => "qqq"
"totalPoints" => 4
"percent" => 0.0
"id" => 6
]
[
"courseId" => 1
"courseDisplayName" => "ips"
"totalPoints" => 4
"percent" => 0.0
"id" => 5
]
]

Here's one way to do it:
Assuming your data is:
$newResponse = [
[
"courseId" => 18,
"courseDisplayName" => "qqq"
],
[
"courseId" => 1,
"courseDisplayName" => "ips",
],
[
"courseId" => 18,
"courseDisplayName" => "qqq",
],
[
"courseId" => 1,
"courseDisplayName" => "ips",
]
];
$key = [
[
"totalPoints" => 2,
"percent" => 1.0,
"id" => 2
],
[
"totalPoints" => 10,
"percent" => 2,
"id" => 3
],
[
"totalPoints" => 4,
"percent" => 0.0,
"id" => 6
],
[
"totalPoints" => 4,
"percent" => 0.0,
"id" => 5
]
];
Then I'd probably reach for array_map, having used it for this kind of purpose recently:
$out = [];
array_map(function($a,$b) use (&$out) {
$out[] = $a + $b;
},$newResponse,$key);
print_r($out);
Note in the above the + is very similar to array merge and I believe interchangeable in your use case. You can read about the difference here: Array_merge versus +

Related

Laravel - Can i add new item to an array based on the `id` value existing that array?

array (
0 =>
array (
'courseId' => 14,
'tutorName' => 'admin',
),
1 =>
array (
'courseId' => 15,
'tutorName' => 'merl',
),
)
var_export($response) is an array like above. For each courseId in the $response array, i wanted to find sum of points when the courseId in the $response array exists student_learning table also. After that, i wanted to add this sum of points($points) to the $response array as a new item for the corresponding courseId. Here, the issue is that, every values of $points is added as a new item in every datasets of the $response array,but i wanted it to get added to the $response array of it's respective courseId only. How can i do that?
foreach ($response as $key ) {
$points=DB::table('student_learning')->groupBy('courseId')->where('courseId',$key['courseId'])->sum('points');
$res = array_map(function($e) use($points,$percent) {
$e['points'] = $points;
return $e; }, $response);
dump($res);
}
dump($res) gives an output like below
array:2 [
0 => array:8 [
"courseId" => 14
"tutorName" => "admin"
"points" => 12
]
1 => array:8 [
"courseId" => 15
"tutorName" => "me"
"points" => 12
]
]
array:2 [
0 => array:8 [
"courseId" => 14
"tutorName" => "admin"
"points" => 3
]
1 => array:8 [
"courseId" => 15
"tutorName" => "me"
"points" => 3
]
]
dump($res) outside the foreach gives an output like below
array:2 [
0 => array:8 [
"courseId" => 14
"tutorName" => "admin"
"points" => 3
]
1 => array:8 [
"courseId" => 15
"tutorName" => "me"
"points" => 3
]
]
Expected/Required Output:
[
"courseId" => 14
"tutorName" => "admin"
"points" => 12
]
[
"courseId" => 15
"tutorName" => "me"
"points" => 3
]
You can add new array key while looping using foreach
$response = array (
0 =>
array (
'courseId' => 14,
'tutorName' => 'admin',
),
1 =>
array (
'courseId' => 15,
'tutorName' => 'merl',
),
);
$newResponse = [];
foreach($response as $eachResponse){
$points=DB::table('student_learning')->groupBy('courseId')->where('courseId',$eachResponse['courseId'])->sum('points');
$eachResponse['points'] = $points;
$newResponse[] = $eachResponse;
}
dd($newResponse);

PHP combine multi-dimensional arrays

I'm have the following array.
"rent" => array:3 [
0 => array:1 [
0 => "5000"
]
1 => array:3 [
0 => "10000"
1 => "60000"
2 => "80000"
]
2 => []
]
"house_quantity" => array:3 [
0 => array:1 [
0 => "2"
]
1 => array:3 [
0 => "3"
1 => "4"
2 => "6"
]
2 => []
]
"property_id" => array:3 [
0 => 1
1 => 2
2 => 3
]
"type_of_house" => array:3 [
0 => array:1 [
0 => array:1 [
"type" => "studio_apartment"
]
]
1 => array:3 [
0 => array:1 [
"type" => "studio_apartment"
]
1 => array:1 [
"type" => "one_bedroom"
]
2 => array:1 [
"type" => "two_bedroom"
]
]
2 => array:2 [
0 => array:1 [
"type" => "studio_apartment"
]
1 => array:1 [
"type" => "two_bedroom"
]
]
]
]
I want to combine the above array so that it forms an array that looks like this.
"0" => [
"property_id" => 1
"type_of_house" => array:3 [
"type"=> "studio_apartment"
"rent" => "5000"
"house_quantity" => "2"
]
]
"1" => [
"property_id" => 2
"type_of_house" => array:3 [
"type"=> "studio_apartment"
"rent" => "10000"
"house_quantity" => "3"
]
"type_of_house" => array:3 [
"type"=> "one_bedroom"
"rent" => "60000"
"house_quantity" => "4"
]
"type_of_house" => array:3 [
"type"=> "two_bedroom"
"rent" => "80000"
"house_quantity" => "6"
]
]
So far I'm using the foreach loop to loop over the properties and attach the type of houses in each of those properties as follows:
foreach ($request->property_id as $key=> $property_id) {
$result[$key] = array(
'property_id' => $property_id,
'type_of_house' => $request->type_of_house[$key]
);
foreach ($result as $property_key => $property) {
foreach ($property['type_of_house'] as $house_key => $house) {
$house[$key][$house_key] = array(
'rent' => $request->rent[$key][$house_key],
'house_quantity' => $request->house_quantity[$key][$house_key]
);
}
}
$merge = array_merge_recursive($result, $house);
dd($merge);
}
But the array I'm getting back is not quite right. This is the array that I'm getting back.
array:3 [
0 => array:2 [
"property_id" => 1
"type_of_house" => array:1 [
0 => array:1 [
"type" => "studio_apartment"
]
]
]
"type" => "studio_apartment"
1 => array:1 [
0 => array:2 [
"rent" => "5000"
"house_quantity" => "2"
]
]
]
How do I correctly merge such an array, thanks.
Ok, so let's suppose you have 2 arrays
$array1:
Array(2){
[number] => 1,
[address] => "Park Ave 273",
[name] => "Peter Jones"
}
And then a clean $array2, the one i'm gonna be putting my info
To mix them i would have to specify the index i want the first array to get in, for example:
$array1 = array(
"number" => 1,
"address" => "Park Ave 273",
"name" => "Peter Jones"
);
$array2 = array();
$array2['client'] = $array1;
Would return:
Array
(
[client] => Array
(
[number] => 1
[address] => Park Ave 273
[name] => Peter Jones
)
)
In case you have more than one client (on this example) you have to do a foreach loop for every client.

Map Array in child in php

I have 2 arrays in php which look like following:
array1 = [product_id = [data], product_id = [data], ..]
[
101 => [
"sku" => "AB01"
],
201 => [
"sky" => "AB02"
],
...
]
array2 = attribute of product with product_id
[
0 => [
"product_id" => 101,
"name" => "pro 1"
],
1 => [
"product_id" => 101,
"size" => "S"
],
2 => [
"product_id" => 201,
"name" => "pro 2"
],
3 => [
"product_id" => 201,
"size" => "S"
],
...
]
What I want is according to product_id in array2 data is pushed in array1 as child array like this
[
101 => [
"sku" => "AB01",
"attributes" => [
0 => [
"product_id" => 101,
"name" => "pro 1"
],
1 => [
"product_id" => 101,
"size" => "S"
]
]
],
201 => [
"sky" => "AB02",
"attributes" => [
0 => [
"product_id" => 201,
"name" => "pro 2"
],
1 => [
"product_id" => 201,
"size" => "S"
]
]
],
...
]
Array length is around 1000 for array1 and >5000 for array2. foreach loop taking too much time. Is there any fast way to achieve it?
About the fastest way I can think of achieving this is using a foreach loop over the second array, as your first array is indexed by the product_id, it is easy to directly insert the new data on each loop...
foreach ( $array2 as $item ) {
$array1[$item['product_id']]['attributes'][] = $item;
}
Maybe it helps you
$array1 = [
101 => [
"sku" => "AB01"
],
201 => [
"sky" => "AB02"
]
];
$array2 = [
0 => [
"product_id" => 101,
"name" => "pro 1"
],
1 => [
"product_id" => 101,
"size" => "S"
],
2 => [
"product_id" => 201,
"name" => "pro 2"
],
3 => [
"product_id" => 201,
"size" => "S"
]
] ;
foreach ($array2 as $result){
if(array_key_exists($result['product_id'], $array1)) {
$array1[$result['product_id']]['attributes'][] = $result;
}
}
print_r($array1);
your result look like this
Array
(
[101] => Array
(
[sku] => AB01
[attributes] => Array
(
[0] => Array
(
[product_id] => 101
[name] => pro 1
)
[1] => Array
(
[product_id] => 101
[size] => S
)
)
)
[201] => Array
(
[sky] => AB02
[attributes] => Array
(
[0] => Array
(
[product_id] => 201
[name] => pro 2
)
[1] => Array
(
[product_id] => 201
[size] => S
)
)
)
)
$arr1=[
101 => [
"sku" => "AB01"
],
201 => [
"sky" => "AB02"
]
];
$arr2=[
0 => [
"product_id" => 101,
"name" => "pro 1"
],
1 => [
"product_id" => 101,
"size" => "S"
],
2 => [
"product_id" => 201,
"name" => "pro 2"
],
3 => [
"product_id" => 201,
"size" => "S"
]
];
foreach ($arr2 as $arr_val ) {
$arr1[$arr_val['product_id']]['attributes'][] = $arr_val;
}
echo "<pre>"; print_r($arr1);
I hope this answer work for you
first array
$arr = [
101 => [
"sku" => "AB01"
],
201 => [
"sky" => "AB02"
],
];
second array
$arr2 = [
0 => [
"product_id" => 101,
"name" => "pro 1"
],
1 => [
"product_id" => 101,
"size" => "S"
],
2 => [
"product_id" => 201,
"name" => "pro 2"
],
3 => [
"product_id" => 201,
"size" => "S"
],
];
finally this what i try to do
$newArr = [];
forEach($arr as $product_id => $product_value){
$attrArr = [];
forEach($arr2 as $key => $value){
if($product_id == $value['product_id']){
$attrArr[] = $value;
$newArr[$product_id] = [
array_keys($product_value)[0] => array_values($product_value)[0],
];
}
}
$newArr[$product_id]['attributes'] = $attrArr;
}
echo '<pre>';
print_r($newArr);
echo '</pre>';

How to get the duplicated items in the array and select one of them to be presented in the new array in laravel

I am building custom permissions system for different sections of a Laravel application.
I have an array of arrays and maybe there is a duplication in the section_id value but with different permission value.
for example i have section_id => 10 exists 3 times here with different 3 permission.
[
0 => array:2 [▼
"section_id" => 10
"permission" => "B"
]
1 => array:2 [▼
"section_id" => 22
"permission" => "A"
]
2 => array:2 [▼
"section_id" => 10
"permission" => "A"
]
3 => array:2 [▼
"section_id" => 13
"permission" => "B"
]
4 => array:2 [▼
"section_id" => 18
"permission" => "B"
]
5 => array:2 [▼
"section_id" => 10
"permission" => "C"
]
]
Now I need to get only the highest permission level available in the array for the duplicated section section => 10
A,B and C are the permissions levels so the priority for A then B then C for the same section , the final result should be like this
[
0 => array:2 [▼
"section_id" => 10
"permission" => "A"
]
1 => array:2 [▼
"section_id" => 22
"permission" => "A"
]
2 => array:2 [▼
"section_id" => 13
"permission" => "B"
]
3 => array:2 [▼
"section_id" => 18
"permission" => "B"
]
]
You can use collections using sortBy(), unique() and values() chain in this way:
public function test() {
$p = [
[
'section_id' => 10,
'permission' => 'B'
],
[
'section_id' => 22,
'permission' => 'A'
],
[
'section_id' => 10,
'permission' => 'A'
],
[
'section_id' => 13,
'permission' => 'B'
],
[
'section_id' => 18,
'permission' => 'B'
],
[
'section_id' => 10,
'permission' => 'C'
],
];
$collection = collect($p);
$sorted = $collection->sortBy(function ($product, $key) {
return ord($product['permission']);
})->unique('section_id')->values()->all();
dd($p, $sorted);
}
Outputs
array:6 [▼
0 => array:2 [▼
"section_id" => 10
"permission" => "B"
]
1 => array:2 [▼
"section_id" => 22
"permission" => "A"
]
2 => array:2 [▼
"section_id" => 10
"permission" => "A"
]
3 => array:2 [▼
"section_id" => 13
"permission" => "B"
]
4 => array:2 [▼
"section_id" => 18
"permission" => "B"
]
5 => array:2 [▼
"section_id" => 10
"permission" => "C"
]
]
array:4 [▼
0 => array:2 [▼
"section_id" => 22
"permission" => "A"
]
1 => array:2 [▼
"section_id" => 10
"permission" => "A"
]
2 => array:2 [▼
"section_id" => 13
"permission" => "B"
]
3 => array:2 [▼
"section_id" => 18
"permission" => "B"
]
]
Try this:
$myArray = array(
["section_id" => 10, "permission" => "B"],
["section_id" => 22, "permission" => "A"],
["section_id" => 10, "permission" => "A"],
["section_id" => 13, "permission" => "B"],
["section_id" => 18, "permission" => "B"],
["section_id" => 10, "permission" => "C"]);
$sectionArr = array_column($myArray, 'section_id');
$permissionArr = array_column($myArray, 'permission');
array_multisort($sectionArr, SORT_ASC, $permissionArr, SORT_ASC, $myArray);
$newArray = array();
$tempSection = array();
foreach($myArray as $key => $singleArray){
if(!in_array($singleArray['section_id'], $tempSection)){
$tempSection[] = $singleArray['section_id'];
$newArray[] = ["section_id" => $singleArray['section_id'], "permission" => $singleArray['permission']];
}
}
print_r($newArray);

PHP usort() not accurate?

I have a multi-dimensional array. Inside each array, their is a sub value called "elo-rating". I want to sort my array based on this sub value.
So I take my array, called $newRanks and run it through the following:
uasort($newRanks, create_function(
'$b, $a', 'return $a["elo_rating"] - $b["elo_rating"];'
));
After running through that code, my array returns as follows:
array:52 [
"3-1154" => array:4 [
"league_id" => 3
"user_id" => 1154
"elo_matches" => 8
"elo_rating" => 1224.47797881
]
"3-205" => array:4 [
"league_id" => 3
"user_id" => 205
"elo_matches" => 11
"elo_rating" => 1207.86593741
]
"3-1" => array:4 [
"league_id" => 3
"user_id" => 1
"elo_matches" => 17
"elo_rating" => 1206.60264689
]
"3-285" => array:4 [
"league_id" => 3
"user_id" => 285
"elo_matches" => 4
"elo_rating" => 1187.31524255
]
"3-259" => array:4 [
"league_id" => 3
"user_id" => 259
"elo_matches" => 4
"elo_rating" => 1173.02391767
]
"3-12689" => array:4 [
"league_id" => 3
"user_id" => 12689
"elo_matches" => 4
"elo_rating" => 1167.46830619
]
"3-1603" => array:4 [
"league_id" => 3
"user_id" => 1603
"elo_matches" => 13
"elo_rating" => 1153.3060092
]
"3-16" => array:4 [
"league_id" => 3
"user_id" => 16
"elo_matches" => 7
"elo_rating" => 1146.65083202
]
"3-1609" => array:4 [
"league_id" => 3
"user_id" => 1609
"elo_matches" => 3
"elo_rating" => 1122.679103
]
"3-333" => array:4 [
"league_id" => 3
"user_id" => 333
"elo_matches" => 5
"elo_rating" => 1112.56694511
]
"3-10030" => array:4 [
"league_id" => 3
"user_id" => 10030
"elo_matches" => 4
"elo_rating" => 1091.27782914
]
"3-378" => array:4 [
"league_id" => 3
"user_id" => 378
"elo_matches" => 9
"elo_rating" => 1082.0354022
]
"3-6107" => array:4 [
"league_id" => 3
"user_id" => 6107
"elo_matches" => 5
"elo_rating" => 1059.74850166
]
"3-5179" => array:4 [
"league_id" => 3
"user_id" => 5179
"elo_matches" => 3
"elo_rating" => 1046.60181418
]
"3-1476" => array:4 [
"league_id" => 3
"user_id" => 1476
"elo_matches" => 9
"elo_rating" => 1038.88789903
]
"3-70" => array:4 [
"league_id" => 3
"user_id" => 70
"elo_matches" => 8
"elo_rating" => 1038.63959146
]
"3-303" => array:4 [
"league_id" => 3
"user_id" => 303
"elo_matches" => 7
"elo_rating" => 1039.26666217
]
"3-59" => array:4 [
"league_id" => 3
"user_id" => 59
"elo_matches" => 1
"elo_rating" => 1033.78309445
]
"3-1017" => array:4 [
"league_id" => 3
"user_id" => 1017
"elo_matches" => 4
"elo_rating" => 1002.79264647
]
"3-632" => array:4 [
"league_id" => 3
"user_id" => 632
"elo_matches" => 3
"elo_rating" => 1002.2039368
]
"3-177" => array:4 [
"league_id" => 3
"user_id" => 177
"elo_matches" => 4
"elo_rating" => 994.838857477
]
"3-12466" => array:4 [
"league_id" => 3
"user_id" => 12466
"elo_matches" => 4
"elo_rating" => 994.761652125
]
"3-9725" => array:4 [
"league_id" => 3
"user_id" => 9725
"elo_matches" => 7
"elo_rating" => 994.520367143
]
"3-1593" => array:4 [
"league_id" => 3
"user_id" => 1593
"elo_matches" => 4
"elo_rating" => 987.448354356
]
"3-78" => array:4 [
"league_id" => 3
"user_id" => 78
"elo_matches" => 16
"elo_rating" => 984.927509938
]
"3-20837" => array:4 [
"league_id" => 3
"user_id" => 20837
"elo_matches" => 4
"elo_rating" => 981.533602402
]
"3-25" => array:4 [
"league_id" => 3
"user_id" => 25
"elo_matches" => 3
"elo_rating" => 977.651701927
]
"3-2056" => array:4 [
"league_id" => 3
"user_id" => 2056
"elo_matches" => 8
"elo_rating" => 978.374247502
]
"3-14300" => array:4 [
"league_id" => 3
"user_id" => 14300
"elo_matches" => 9
"elo_rating" => 958.218292232
]
"3-16900" => array:4 [
"league_id" => 3
"user_id" => 16900
"elo_matches" => 3
"elo_rating" => 957.66758785
]
"3-5" => array:4 [
"league_id" => 3
"user_id" => 5
"elo_matches" => 3
"elo_rating" => 955.441682773
]
"3-11793" => array:4 [
"league_id" => 3
"user_id" => 11793
"elo_matches" => 3
"elo_rating" => 956.118019821
]
"3-23" => array:4 [
"league_id" => 3
"user_id" => 23
"elo_matches" => 1
"elo_rating" => 950.0
]
"3-160" => array:4 [
"league_id" => 3
"user_id" => 160
"elo_matches" => 6
"elo_rating" => 946.346810828
]
"3-11882" => array:4 [
"league_id" => 3
"user_id" => 11882
"elo_matches" => 3
"elo_rating" => 943.113557791
]
"3-178" => array:4 [
"league_id" => 3
"user_id" => 178
"elo_matches" => 3
"elo_rating" => 940.38037017
]
"3-2113" => array:4 [
"league_id" => 3
"user_id" => 2113
"elo_matches" => 3
"elo_rating" => 940.343382565
]
"3-1334" => array:4 [
"league_id" => 3
"user_id" => 1334
"elo_matches" => 2
"elo_rating" => 923.336202927
]
"3-184" => array:4 [
"league_id" => 3
"user_id" => 184
"elo_matches" => 2
"elo_rating" => 920.326252901
]
"3-2162" => array:4 [
"league_id" => 3
"user_id" => 2162
"elo_matches" => 2
"elo_rating" => 917.932985501
]
"3-2058" => array:4 [
"league_id" => 3
"user_id" => 2058
"elo_matches" => 6
"elo_rating" => 905.641833006
]
"3-1951" => array:4 [
"league_id" => 3
"user_id" => 1951
"elo_matches" => 2
"elo_rating" => 906.136056131
]
"3-1749" => array:4 [
"league_id" => 3
"user_id" => 1749
"elo_matches" => 2
"elo_rating" => 905.570092295
]
"3-15296" => array:4 [
"league_id" => 3
"user_id" => 15296
"elo_matches" => 2
"elo_rating" => 901.02829192
]
"3-11684" => array:4 [
"league_id" => 3
"user_id" => 11684
"elo_matches" => 2
"elo_rating" => 901.02829192
]
"3-940" => array:4 [
"league_id" => 3
"user_id" => 940
"elo_matches" => 2
"elo_rating" => 899.735074733
]
"3-12235" => array:4 [
"league_id" => 3
"user_id" => 12235
"elo_matches" => 2
"elo_rating" => 900.0
]
"3-2957" => array:4 [
"league_id" => 3
"user_id" => 2957
"elo_matches" => 2
"elo_rating" => 900.0
]
"3-14959" => array:4 [
"league_id" => 3
"user_id" => 14959
"elo_matches" => 2
"elo_rating" => 894.798068073
]
"3-779" => array:4 [
"league_id" => 3
"user_id" => 779
"elo_matches" => 5
"elo_rating" => 874.675970857
]
"3-110" => array:4 [
"league_id" => 3
"user_id" => 110
"elo_matches" => 4
"elo_rating" => 849.309123925
]
"3-5837" => array:4 [
"league_id" => 3
"user_id" => 5837
"elo_matches" => 4
"elo_rating" => 821.601462523
]
]
If you look closely at this resulting array, you'll notice that its not exactly sorted properly. For instance, it puts 1039.26666217 below both 1038.63959146 and 1038.88789903.
Any ideas on how to fix this?
You are returning decimal values from your function, which the documentation warns against.
The comparison function must return an integer less than, equal to, or
greater than zero if the first argument is considered to be
respectively less than, equal to, or greater than the second. Note
that before PHP 7.0.0 this integer had to be in the range from
-2147483648 to 2147483647.
Caution Returning non-integer values from the comparison function,
such as float, will result in an internal cast to integer of the
callback's return value. So values such as 0.99 and 0.1 will both be
cast to an integer value of 0, which will compare such values as
equal.
Change your function to return an integer to fix the issue.
uasort($newRanks, create_function(
'$b, $a', 'return ($a["elo_rating"] > $b["elo_rating"])?-1:1;'
));
Two problems:
You're using create_function() which uses eval() internally, which is a gaping security hole, and unnecessary to boot. Just use an actual anonymous function.
Your function arguments are backwards.
Willem Renzema raises a valid point about the implicit cast of the return value, but I don't like the solution.
So:
$epsilon = 0.000000001;
uasort(
$newRanks,
function($a,$b)use($epsilon}{
$diff = $a["elo_rating"] - $b["elo_rating"];
if( abs($diff) < $epsilon ) { return 0; }
else if( $diff > 0 ) { return 1; }
else { return -1; }
)
);
Where $epsilon is a value chosen specifically for this comparison where you consider any $diff smaller than that to be an equivalence, aka float/rounding error.
See: What is the most effective way for float and double comparison?

Categories