Parsing conditions in a custom DSL using a tokenizer - php

I am trying to parse a "rules string" provided over an API in PHP.
Given that I have an array of items such as
$items = [41, 42, 51, 54, 65, 12];
I wish to evaluate this expression
({41} AND {51}) OR ({31} AND {42})
In this instance it should return true, since the $items array has both the 41 and 51 items.
I started with creating a tokenizer using the https://github.com/nette/tokenizer library and currently have it parsing the string into an array of tokens.
array:25 [
0 => array:3 [
0 => "("
1 => 0
2 => 1
]
1 => array:3 [
0 => "{"
1 => 1
2 => 2
]
2 => array:3 [
0 => "41"
1 => 2
2 => 3
]
3 => array:3 [
0 => "}"
1 => 4
2 => 4
]
4 => array:3 [
0 => " "
1 => 5
2 => 8
]
5 => array:3 [
0 => "AND"
1 => 6
2 => 5
]
6 => array:3 [
0 => " "
1 => 9
2 => 8
]
7 => array:3 [
0 => "{"
1 => 10
2 => 2
]
8 => array:3 [
0 => "51"
1 => 11
2 => 3
]
9 => array:3 [
0 => "}"
1 => 13
2 => 4
]
10 => array:3 [
0 => ")"
1 => 14
2 => 7
]
11 => array:3 [
0 => " "
1 => 15
2 => 8
]
12 => array:3 [
0 => "OR"
1 => 16
2 => 6
]
13 => array:3 [
0 => " "
1 => 18
2 => 8
]
14 => array:3 [
0 => "("
1 => 19
2 => 1
]
15 => array:3 [
0 => "{"
1 => 20
2 => 2
]
16 => array:3 [
0 => "31"
1 => 21
2 => 3
]
17 => array:3 [
0 => "}"
1 => 23
2 => 4
]
18 => array:3 [
0 => " "
1 => 24
2 => 8
]
19 => array:3 [
0 => "AND"
1 => 25
2 => 5
]
20 => array:3 [
0 => " "
1 => 28
2 => 8
]
21 => array:3 [
0 => "{"
1 => 29
2 => 2
]
22 => array:3 [
0 => "42"
1 => 30
2 => 3
]
23 => array:3 [
0 => "}"
1 => 32
2 => 4
]
24 => array:3 [
0 => ")"
1 => 33
2 => 7
]
]
However this is where my talent runs out and I am not sure how to process the tokens so that I can run the condition against my $items array.
It could be that I am approaching this completely wrong.

Related

filter associative array that has another array inside it

I have this $data, which holds an array of records from schedules table.
If you want to see the code, please check this repository from LaravelDaily, it's the same code I'm working on.
"07:00 AM - 07:05 AM" => array:5 [▼
0 => 1
1 => 1
2 => 1
3 => 1
4 => 1
]
"07:05 AM - 07:10 AM" => array:5 [▼
0 => 1
1 => 1
2 => 1
3 => 1
4 => 1
]
"07:10 AM - 07:15 AM" => array:5 [▼
0 => 1
1 => 1
2 => 1
3 => 1
4 => 1
]
"07:15 AM - 07:20 AM" => array:5 [▼
0 => 1
1 => 1
2 => array:3 [▼
"schedule_id" => 10
"time" => "07:15 AM - 07:20 AM"
"subject_name" => "Subject Abc"
]
3 => 1
4 => 1
]
I want to only return/show the array that has another array inside it and remove the others like what you will see below.
In this case it's 07:15 AM - 07:20 AM
"07:15 AM - 07:20 AM" => array:5 [▼
0 => 1
1 => 1
2 => array:3 [▼
"schedule_id" => 10
"time" => "07:15 AM - 07:20 AM"
"subject_name" => "Subject Abc"
]
3 => 1
4 => 1
]
Is that possible without using a foreach loop?
What I use:
Laravel 9.33
PHP 8.1
You can use array_filter by a custom callback function.
$list = [
"07:00 AM - 07:05 AM" => [
0 => 1,
1 => 1,
2 => 1,
3 => 1,
4 => 1,
],
"07:05 AM - 07:10 AM" => [
0 => 1,
1 => 1,
2 => 1,
3 => 1,
4 => 1,
],
"07:10 AM - 07:15 AM" => [
0 => 1,
1 => 1,
2 => 1,
3 => 1,
4 => 1,
],
"07:15 AM - 07:20 AM" => [
0 => 1,
1 => 1,
2 => [
"schedule_id" => 10,
"time" => "07:15 AM - 07:20 AM",
"subject_name" => "Subject Abc",
],
3 => 1,
4 => 1,
]
];
function myFunc($arr) {
return array_filter($arr, 'is_array');
}
$filtered = array_filter($list, 'myFunc');
var_dump($filtered);
/**
Output:
Array
(
[07:15 AM - 07:20 AM] => Array
(
[0] => 1
[1] => 1
[2] => Array
(
[schedule_id] => 10
[time] => 07:15 AM - 07:20 AM
[subject_name] => Subject Abc
)
[3] => 1
[4] => 1
)
)
*/
Edited:
By return array_filter($arr, 'is_array'); as a callback function its can extract the array on any index, not only index 2.
Because you are already using Laravel, you might want to try this:
collect($data)->filter(function ($item) {
return collect($item)->contains(function ($subItem) {
return is_array($subItem);
})
});

Multiple sums by created_at date with union and group and sort by months with mysql

I have client table what I want sum new client (separate by client type) group by months.
I have wrote this query:
$newClient = Client::select(
DB::raw("DATE_FORMAT(clients.created_at,'%M %Y') as months"),
DB::raw("COUNT(*) as new_clients, 0 as new_private_person_clients, 0 as company_clients")
)->groupBy('months');
$newPrivatePersonClients = Client::select(
DB::raw("DATE_FORMAT(clients.created_at,'%M %Y') as months"),
DB::raw("0 as new_clients, COUNT(*) as new_private_person_clients, 0 as company_clients")
)->filterByType(ClientType::PRIVATE_PERSON)
->groupBy('months');
$newCompanyClients = Client::select(
DB::raw("DATE_FORMAT(clients.created_at,'%M %Y') as months"),
DB::raw("0 as new_clients, 0 as new_private_person_clients, COUNT(*) as company_clients")
)->filterByType(ClientType::COMPANY)
->groupBy('months')->union($newClient)->union($newPrivatePersonClients)->orderBy('months')->get()->toArray();
... and the result array:
array:8 [
0 => array:4 [
"months" => "April 2022"
"new_clients" => 0
"new_private_person_clients" => 0
"company_clients" => 3
]
1 => array:4 [
"months" => "April 2022"
"new_clients" => 7
"new_private_person_clients" => 0
"company_clients" => 0
]
2 => array:4 [
"months" => "April 2022"
"new_clients" => 0
"new_private_person_clients" => 4
"company_clients" => 0
]
3 => array:4 [
"months" => "June 2022"
"new_clients" => 3
"new_private_person_clients" => 0
"company_clients" => 0
]
4 => array:4 [
"months" => "June 2022"
"new_clients" => 0
"new_private_person_clients" => 3
"company_clients" => 0
]
5 => array:4 [
"months" => "May 2022"
"new_clients" => 0
"new_private_person_clients" => 0
"company_clients" => 1
]
6 => array:4 [
"months" => "May 2022"
"new_clients" => 3
"new_private_person_clients" => 0
"company_clients" => 0
]
7 => array:4 [
"months" => "May 2022"
"new_clients" => 0
"new_private_person_clients" => 2
"company_clients" => 0
]
]
...maybe you see the problems :)
I would like get this:
0 => array:4 [
"months" => "April 2022"
"new_clients" => 7
"new_private_person_clients" => 4
"company_clients" => 3
]
1 => array:4 [
"months" => "May 2022"
"new_clients" => 3
"new_private_person_clients" => 2
"company_clients" => 1
]
2 => array:4 [
"months" => "June 2022"
"new_clients" => 3
"new_private_person_clients" => 3
"company_clients" => 0
]
Important! There are months without company or private person clients.
Maybe I work the got array, but maybe there is other query way for this result.
(php 7.4, mysql 8x, Laravel 6)
Thanks,

Merging Multi-dimensional Array (Custom) PHP - Laravel

Merge a multi-dimensional array to the custom array as required.
Need the solution in PHP (using in Laravel-8).
Custom array is needed to make rows for creating an excel file using Spreadsheet.
This is the Original array I have =
array:3 [
0 => array:4 [
0 => array:3 [
0 => "Name"
1 => "English"
2 => "Math"
]
1 => array:3 [
0 => "John"
1 => 10
2 => 20
]
2 => array:3 [
0 => "Doe"
1 => 20
2 => 30
]
3 => array:3 [
0 => "Smith"
1 => 30
2 => 50
]
]
1 => array:4 [
0 => array:3 [
0 => "Name"
1 => "Science"
2 => "Hindi"
]
1 => array:3 [
0 => "John"
1 => 10
2 => 20
]
2 => array:3 [
0 => "Doe"
1 => 20
2 => 57
]
3 => array:3 [
0 => "Smith"
1 => 30
2 => 89
]
]
2 => array:4 [
0 => array:3 [
0 => "Name"
1 => "ABC"
2 => "XYZ"
]
1 => array:3 [
0 => "John"
1 => 10
2 => 20
]
2 => array:3 [
0 => "Doe"
1 => 20
2 => 23
]
3 => array:3 [
0 => "Smith"
1 => 30
2 => 89
]
]
]
From the above array need to make the array like below (array size may very, so need dynamic solutions) -
array:1 [
0 => array:4 [
0 => array:7 [
0 => "Name"
1 => "English"
2 => "Math"
3 => "Science"
4 => "Hindi"
5 => "ABC"
6 => "XYZ"
]
1 => array:7 [
0 => "John"
1 => 10
2 => 20
3 => 10
4 => 20
5 => 10
6 => 20
]
2 => array:7 [
0 => "Doe"
1 => 20
2 => 30
3 => 20
4 => 57
5 => 20
6 => 23
]
3 => array:7 [
0 => "Smith"
1 => 30
2 => 50
3 => 30
4 => 89
5 => 30
6 => 89
]
]
]

Comparing two associative arrays values and replacing them

I'm facing issue while dealing with 2 associative arrays. I have two arrays, If id of Array 1 = id of Array 2 = id, then active_lession and active_learning_lession of Array 1 should be replaced by active_lession and active_learning_lession of Array 2 respectively.
Array 1 =>
array:3 [
0 => array:10 [
"id" => 3
"status" => "1"
"active_lession" => 0
"active_learning_lession" => 0
"learninglessions" => array:2 [
0 => array:12 [
"id" => 2
"language_id" => 1
"category_id" => 3
"sentence" => "अगर आपको अपना स्कूल का नाम पुछा जाए तो क्या कहेंगे"
"english_sentence" => "I am student of …… School."
]
1 => array:12 [
"id" => 27
"language_id" => 1
"category_id" => 3
"sentence" => "यह मेरा दोस्त/मित्र है"
"english_sentence" => "He is my friend."
]
]
]
1 => array:10 [
"id" => 4
"name" => "Module 2"
"image" => "public/icon/downloadxxx.jpeg"
"status" => "1"
"active_lession" => 10
"active_learning_lession" => 0
"learninglessions" => array:2 [
0 => array:12 [
"id" => 1
"language_id" => 1
"category_id" => 4
"sentence" => "अपना परिचय कैसे देंगे –"
"english_sentence" => "I am..."
]
]
]
2 => array:10 [
"id" => 5
"status" => "1"
"active_lession" => 0
"active_learning_lession" => 0
"learninglessions" => array:4 [
0 => array:12 [
"id" => 29
"language_id" => 1
"category_id" => 5
"sentence" => "यह एक बाग है ।"
"english_sentence" => "This is a Park."
]
1 => array:12 [
"id" => 34
"language_id" => 1
"category_id" => 5
"sentence" => "कैसा चल रहा है ?"
"english_sentence" => "How are things ?"
]
]
]
Array 2=>
array:3 [
0 => array:3 [
"id" => 3
"active_learning_lession" => 25
"active_lession" => 20
]
1 => array:3 [
"id" => 4
"active_learning_lession" => 20
"active_lession" => 15
]
]
Thus Expected Array will be
array:3 [
0 => array:10 [
"id" => 3
"status" => "1"
"active_lession" => 20
"active_learning_lession" => 25
"learninglessions" => array:2 [
0 => array:12 [
"id" => 2
"language_id" => 1
"category_id" => 3
"sentence" => "अगर आपको अपना स्कूल का नाम पुछा जाए तो क्या कहेंगे"
"english_sentence" => "I am student of …… School."
]
1 => array:12 [
"id" => 27
"language_id" => 1
"category_id" => 3
"sentence" => "यह मेरा दोस्त/मित्र है"
"english_sentence" => "He is my friend."
]
]
]
1 => array:10 [
"id" => 4
"name" => "Module 2"
"image" => "public/icon/downloadxxx.jpeg"
"status" => "1"
"active_lession" => 15
"active_learning_lession" => 20
"learninglessions" => array:2 [
0 => array:12 [
"id" => 1
"language_id" => 1
"category_id" => 4
"sentence" => "अपना परिचय कैसे देंगे –"
"english_sentence" => "I am..."
]
]
]
2 => array:10 [
"id" => 5
"status" => "1"
"active_lession" => 0
"active_learning_lession" => 0
"learninglessions" => array:4 [
0 => array:12 [
"id" => 29
"language_id" => 1
"category_id" => 5
"sentence" => "यह एक बाग है ।"
"english_sentence" => "This is a Park."
]
1 => array:12 [
"id" => 34
"language_id" => 1
"category_id" => 5
"sentence" => "कैसा चल रहा है ?"
"english_sentence" => "How are things ?"
]
]
]
Try this code..
$res = [];
foreach($x as $key => $xx)
{
foreach($y as $k => $yy)
{
if($xx['id'] == $yy['id'])
{
$res[$key] = $xx;
$res[$key]['active_lession'] = $yy['active_lession'];
$res[$key]['active_learning_lession'] = $yy['active_learning_lession'];
}
else
{
if(!array_key_exists($key,$res))
{
$res[$key] = $xx;
}
}
}
}
print_r($res);
OR
foreach($x as $key => $value)
{
foreach($y as $yy)
{
if($value['id'] == $yy['id'])
{
$x[$key]['active_learning_lession'] = $yy['active_learning_lession'];
$x[$key]['active_lession'] = $yy['active_lession'];
}
}
}
print_r($x);
Output will be
Array
(
[0] => Array
(
[id] => 3
[status] => 1
[active_lession] => 20
[active_learning_lession] => 25
[learninglessions] => Array
(
[0] => Array
(
[id] => 2
[language_id] => 1
[category_id] => 3
[sentence] => jhdbfhbs
[english_sentence] => I am student of …… School.
)
[1] => Array
(
[id] => 27
[language_id] => 1
[category_id] => 3
[sentence] => dbshbfjhf
[english_sentence] => He is my friend.
)
)
)
[1] => Array
(
[id] => 4
[name] => Module 2
[image] => public/icon/downloadxxx.jpeg
[status] => 1
[active_lession] => 15
[active_learning_lession] => 20
[learninglessions] => Array
(
[0] => Array
(
[id] => 1
[language_id] => 1
[category_id] => 4
[sentence] => jhbdsfhjferu
[english_sentence] => I am...
)
)
)
[2] => Array
(
[id] => 5
[status] => 1
[active_lession] => 0
[active_learning_lession] => 0
[learninglessions] => Array
(
[0] => Array
(
[id] => 29
[language_id] => 1
[category_id] => 5
[sentence] => jbfhgbdu
[english_sentence] => This is a Park.
)
[1] => Array
(
[id] => 34
[language_id] => 1
[category_id] => 5
[sentence] => jhsbdhjfbuyefr
[english_sentence] => How are things ?
)
)
)
)

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