I have the following code:
function create_sort_callback($criteria)
{
return function($a, $b)
{
$a = $a[$criteria];
$b = $b[$criteria];
return ($a == $b ? 0 : (($a < $b) ? -1 : 1));
};
}
It turns out I can't access $criteria from within the inner function. How can I solve this problem?
Try like this
function create_sort_callback($criteria)
{
return function($a, $b) use($criteria)
{
$a = $a[$criteria];
$b = $b[$criteria];
return ($a == $b ? 0 : (($a < $b) ? -1 : 1));
};
}
You need using closures http://www.php.net/manual/en/functions.anonymous.php
Use the use keyword.
function create_sort_callback($criteria)
{
return function($a, $b) use ($criteria)
{
$a = $a[$criteria];
$b = $b[$criteria];
return ($a == $b ? 0 : (($a < $b) ? -1 : 1));
};
}
Related
I need to refactor a piece of php code to work with 7.2 but i am not sure if the way i did it is correct. I could use some info about it.
This is the old code
private function sortByFields($field, &$array)
{
usort($array, create_function('$a, $b', '
$a = $a["' . $field . '"];
$b = $b["' . $field . '"];
if ($a == $b) return 0;
return ($a < $b) ? -1 : 1;
'));
return true;
}
And this is the code i refactored for 7.2
private function sortByFields($field, &$array)
{
usort($array, function ($a,$b) {
$a = $a["' . $field . '"];
$b = $b["' . $field . '"];
if ($a == $b) return 0;
return ($a < $b) ? -1 : 1;
});
return true;
}
Is it correct or did i mess up ?
You missed out on the "use" part so the function body does not know about the $field and messed up the $field array keys - see below:
private function sortByFields($field, &$array)
{
usort($array, function ($a,$b) use ($field) {
$a = $a[$field];
$b = $b[$field];
if ($a == $b) return 0;
return ($a < $b) ? -1 : 1;
});
return true;
}
Say I have an array
$ar = ['apples','blueberries','end','pears','dragonfruit','oranges','start','durian','lychee','rambutan','pineapple','end','start'];
I want the array in some order (lets say alphabetic order for this argument), but with the values 'end' on the tail and 'start' on the head of the array.
function cmp($a,$b) {
if ($a == $b) return 0;
if ($b === 'start') return 1;
if ($b === 'end') return -1;
return ($a < $b) ? -1 : 1;
}
usort($ar,"cmp");
echo implode(", ", $ar);
How do I sort so that values matching a specific value will end up at the head or tail of the array, but other values will sort based on other criteria (e.g. numeric, alpha, etc)
You can use array_diff with sort, array_push and array_unshift
$elements = ['start','end'];//start & end elements array
$rest = array_diff($ar, $elements);
sort($rest);//Sorting of the rest items
array_push($rest, $elements[1]);//Push end element
array_unshift($rest, $elements[0]);//Push start element
You can use rsort($rest) for descending order.
Live Example : https://3v4l.org/GnotC
Try this
$ar = ['apples','blueberries','end','pears','dragonfruit','oranges','start','durian','lychee','rambutan','pineapple','end','start', 'end', 'banana', 'yellow'];
function cmp($a, $b) {
if ($a === $b) {
return 0;
}
if ($a === 'start' || $b === 'end' ) {
return -1;
}
if( $b === 'start' || $a === 'end') {
return 1;
}
return ($a < $b) ? -1 : 1; }
usort($ar,"cmp");
echo implode(', ', $ar);
Hope this will help you
Following is how your cmp function should be. Just a couple of if statements introduced.
function cmp($a, $b) {
if ($a === $b) {
return 0;
}
if ($a === 'start' ) {
return -1;
}
if( $b === 'start' ) {
return 1;
}
if ($a === 'end' ) {
return 1;
}
if ($b === 'end' ) {
return -1;
}
return ($a < $b) ? -1 : 1;
}
I've got a lot of legacy code like this:
return isset($a) ? $a : isset($b) ? $b : isset($c) ? $c : isset($d) ? $d : isset($e) ? $e : '';
So it's just chained ternary operator.
It works, but most of the variable names are not $a, but $veryLongAndDescriptiveCorporateVariableNames and it's just unreadable for me.
Is there a better way to refactor this? I tried something like this but I don't know if it's the best and most readable way:
$ar = array(
$a,
$b,
$c,
$d,
$e
);
foreach ($ar as $k => $v)
if (isset($v))
return $v;
return '';
It's Working fine:-
$ar = array(
$a,
$b,
$c,
$d,
$e
);
foreach ($ar as $k => $v)
{
echo (isset($v)) ? $v : '';
}
Indentation is your friend...
return isset($a)
? $a
: isset($b)
? $b
: isset($c)
? $c
: isset($d)
? $d
: isset($e)
? $e
: '';
M.Fowler would recommend to replace nested conditional with guard clauses:
if (isset($a) {
return $a;
}
if (isset($b) {
return $b;
}
if (isset($c) {
return $c;
}
if (isset($d) {
return $d;
}
if (isset($e) {
return $e;
}
return '';
https://refactoring.guru/replace-nested-conditional-with-guard-clauses
where:
$b = true;
$c = 0;
$a = ($a ? ($a ? $b : $c) : ($c ? $a : $b));
I'm not sure how to work out a.
So I understand that this is a shorthand operator, and usually it's a case of:
$value ? true : false
meaning
if $a = true { true } else { false };
so:
if $a{
if $a{
true;}
else{
0;};
else{
if $0{
$a;}
else{
true;}
};
does this make the value of $a true?
The value of $a would be true
$b = true;
$c = 0;
$a = ($a ? ($a ? $b : $c) : ($c ? $a : $b));
The shorthand can be interpreted like this:
if($a) {
if($a) {
$a = $b;
} else {
$a = $c;
}
} else {
if($c) {
$a = $a;
} else {
$a = $b;
}
}
Because $a is false for not existing in the first place, it immediately jumps to the else statement in that. So the only part that matters to you is:
if($c) {
$a = $a;
} else {
$a = $b;
}
0 is the same as false, so $c will come back as false, therefore $a is equal to $b, which is true.
Edit:
There is some discussion on the notice that is thrown, but this fails to account for the fact that notices are not truly errors and because of this there is no interruption to the code. The result is not Notice: Undefined variable: a, the "result" (think these people mean output) would be blank if it weren't for us determining the value of $a at the end with var_dump. The question was as to what the value of $a becomes, not what appears on your screen.
Something displaying on your screen in re to a variable not being set has nothing to do with the value of what $a is.
If you execute the following code, the notice is not the only thing realized:
$b = true;
$c = 0;
$a = ($a ? ($a ? $b : $c) : ($c ? $a : $b));
var_dump($a);
So the output is:
E_NOTICE : type 8 -- Undefined variable: a -- at line 5
bool(true)
The fact that a notice was thrown does not prevent $a from becoming true.
Also notices are easily suppressed...
error_reporting(0);
$b = true;
$c = 0;
$a = ($a ? ($a ? $b : $c) : ($c ? $a : $b));
var_dump($a);
would result in $a still becoming true, and without seeing the notice.
bool(true)
If you run the code as is, you would get: Notice: Undefined variable: a in myfile.php on line 4
Therefore, I would postulate $a is set somewhere earlier. Yet, whatever value $a has prior, if $a is can be evaluated to true or false, $a would still be true after running your code for the following reason:
If $a were true, then the first part would yield $a = $b and we know $b = true.
if(TRUE) {
if(TRUE) {
$a = $b; //AND $b == TRUE
} else {
$a = $c;
}
} else {
...
}
If $a were false, then the second part would yield $a = $b again
if(FALSE) {
...
} else {
if(0) { // 0 will equate to FALSE
...
} else {
// 0 is the same as FALSE so we end up again with $a = $b
$a = $b; //AND $b == TRUE
}
}
In fact, if you run this code, it will show you the value of $a is true both times:
<?php
$a = false;
$b = true;
$c = 0;
$a = ($a ? ($a ? $b : $c) : ($c ? $a : $b));
echo $a;
$a = true;
$b = true;
$c = 0;
$a = ($a ? ($a ? $b : $c) : ($c ? $a : $b));
echo $a;
Hallo, how can I pass more parameters to usort?
I have different functions, which are very similar in structure, I want to have just one function:
<?php
$arr = array( array('number' => 100, 'string'=>'aaa'),
array('number'=>50, 'string'=>'bdef'),
array('number'=>150, 'string'=>'cbba')
);
usort($arr, 'sortNumberDesc');
//How can I use just a single function?
//How can I pass further parameters to usort?
function sortNumberDesc($a, $b){
$a = $a['number'];
$b = $b['number'];
if ($a == $b) return 0;
return ($a > $b) ? -1 : +1;
}
function sortNumberAsc($a, $b){
$a = $a['number'];
$b = $b['number'];
if ($a == $b) return 0;
return ($a < $b) ? -1 : +1;
}
//I want to do the same with just one function:
//Sort ID is the search index, reverse DESC or ASC
function sort($a, $b, $sortId='number', $reverse = 0){
$a = $a[$sortId];
$b = $b[$sortId];
if ($a == $b) return 0;
if($reverse == false) return ($a > $b) ? -1 : +1;
else return ($a < $b) ? -1 : +1;
}
print_r($arr);
?>
You can use a much simpler function:
function sortNumberAsc($a, $ b){
$a = $a['number'];
$b = $ b['number'];
if ($a == $b) return 0;
return ($a < $b) ? -1 : +1;
}