PHP spaceship sort not working with variable [duplicate]
This question already has an answer here:
Reference: What is variable scope, which variables are accessible from where and what are “undefined variable” errors?
3 answers
I have a sort function with a multidimensional array, but it doesn't work when I use a variable.
This works
function orderdata($l_col, $l_dir, $l_data) {
usort($l_data, function ($a, $b) {
return $a[0] <=> $b[0];
});
return $l_data;
}
This does not work ($l_col is 0)
function orderdata($l_col, $l_dir, $l_data) {
usort($l_data, function ($a, $b) {
return $a[$l_col] <=> $b[$l_col];
});
return $l_data;
}
What is the proper syntax?
php
marked as duplicate by Nigel Ren
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 13:33
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Reference: What is variable scope, which variables are accessible from where and what are “undefined variable” errors?
3 answers
I have a sort function with a multidimensional array, but it doesn't work when I use a variable.
This works
function orderdata($l_col, $l_dir, $l_data) {
usort($l_data, function ($a, $b) {
return $a[0] <=> $b[0];
});
return $l_data;
}
This does not work ($l_col is 0)
function orderdata($l_col, $l_dir, $l_data) {
usort($l_data, function ($a, $b) {
return $a[$l_col] <=> $b[$l_col];
});
return $l_data;
}
What is the proper syntax?
php
marked as duplicate by Nigel Ren
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 13:33
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
$l_col
isn't in scope in the closure, you need touse()
it. See Extending the scope of variables into anonymous functions in the suggested duplicate.
– iainn
Nov 21 '18 at 13:33
add a comment |
This question already has an answer here:
Reference: What is variable scope, which variables are accessible from where and what are “undefined variable” errors?
3 answers
I have a sort function with a multidimensional array, but it doesn't work when I use a variable.
This works
function orderdata($l_col, $l_dir, $l_data) {
usort($l_data, function ($a, $b) {
return $a[0] <=> $b[0];
});
return $l_data;
}
This does not work ($l_col is 0)
function orderdata($l_col, $l_dir, $l_data) {
usort($l_data, function ($a, $b) {
return $a[$l_col] <=> $b[$l_col];
});
return $l_data;
}
What is the proper syntax?
php
This question already has an answer here:
Reference: What is variable scope, which variables are accessible from where and what are “undefined variable” errors?
3 answers
I have a sort function with a multidimensional array, but it doesn't work when I use a variable.
This works
function orderdata($l_col, $l_dir, $l_data) {
usort($l_data, function ($a, $b) {
return $a[0] <=> $b[0];
});
return $l_data;
}
This does not work ($l_col is 0)
function orderdata($l_col, $l_dir, $l_data) {
usort($l_data, function ($a, $b) {
return $a[$l_col] <=> $b[$l_col];
});
return $l_data;
}
What is the proper syntax?
This question already has an answer here:
Reference: What is variable scope, which variables are accessible from where and what are “undefined variable” errors?
3 answers
php
php
edited Nov 21 '18 at 13:39
Nesku
4151311
4151311
asked Nov 21 '18 at 13:30
B. SwansB. Swans
31
31
marked as duplicate by Nigel Ren
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 13:33
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Nigel Ren
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 13:33
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
$l_col
isn't in scope in the closure, you need touse()
it. See Extending the scope of variables into anonymous functions in the suggested duplicate.
– iainn
Nov 21 '18 at 13:33
add a comment |
$l_col
isn't in scope in the closure, you need touse()
it. See Extending the scope of variables into anonymous functions in the suggested duplicate.
– iainn
Nov 21 '18 at 13:33
$l_col
isn't in scope in the closure, you need to use()
it. See Extending the scope of variables into anonymous functions in the suggested duplicate.– iainn
Nov 21 '18 at 13:33
$l_col
isn't in scope in the closure, you need to use()
it. See Extending the scope of variables into anonymous functions in the suggested duplicate.– iainn
Nov 21 '18 at 13:33
add a comment |
2 Answers
2
active
oldest
votes
You'll have to import $l_col
into the closure with the use language construct.
The manual states:
Closures may also inherit variables from the parent scope. Any such
variables must be passed to the use language construct. From PHP 7.1,
these variables must not include superglobals, $this, or variables
with the same name as a parameter.
function orderdata($l_col,$l_dir,$l_data) {
usort($l_data, function($a, $b) use ($l_col) {
return $a[$l_col] <=> $b[$l_col];
});
return $l_data;
}
Great, thanks dn Fer!
– B. Swans
Nov 21 '18 at 13:58
add a comment |
Try the following:
function orderdata($l_col,$l_dir,$l_data) {
usort($l_data, function($a, $b) use $l_col{
return $a[$l_col] <=> $b[$l_col];
});
return $l_data;
}
Note that $l_col
was inaccessible in your anonymous sorting function and you have to pass it explicitly to add it to the scope.
Great, thanks for the responses Nigel and Manoj! The use command was new to me, so i appreciate it.
– B. Swans
Nov 21 '18 at 13:37
1
This answer looks correct but it is not (it doesn't compile). Next time check the correctness of the answer and explain why your answer solves the OP's question. Read more about inheriting variables from the enclosing scope in anonymous functions.
– axiac
Nov 21 '18 at 13:40
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You'll have to import $l_col
into the closure with the use language construct.
The manual states:
Closures may also inherit variables from the parent scope. Any such
variables must be passed to the use language construct. From PHP 7.1,
these variables must not include superglobals, $this, or variables
with the same name as a parameter.
function orderdata($l_col,$l_dir,$l_data) {
usort($l_data, function($a, $b) use ($l_col) {
return $a[$l_col] <=> $b[$l_col];
});
return $l_data;
}
Great, thanks dn Fer!
– B. Swans
Nov 21 '18 at 13:58
add a comment |
You'll have to import $l_col
into the closure with the use language construct.
The manual states:
Closures may also inherit variables from the parent scope. Any such
variables must be passed to the use language construct. From PHP 7.1,
these variables must not include superglobals, $this, or variables
with the same name as a parameter.
function orderdata($l_col,$l_dir,$l_data) {
usort($l_data, function($a, $b) use ($l_col) {
return $a[$l_col] <=> $b[$l_col];
});
return $l_data;
}
Great, thanks dn Fer!
– B. Swans
Nov 21 '18 at 13:58
add a comment |
You'll have to import $l_col
into the closure with the use language construct.
The manual states:
Closures may also inherit variables from the parent scope. Any such
variables must be passed to the use language construct. From PHP 7.1,
these variables must not include superglobals, $this, or variables
with the same name as a parameter.
function orderdata($l_col,$l_dir,$l_data) {
usort($l_data, function($a, $b) use ($l_col) {
return $a[$l_col] <=> $b[$l_col];
});
return $l_data;
}
You'll have to import $l_col
into the closure with the use language construct.
The manual states:
Closures may also inherit variables from the parent scope. Any such
variables must be passed to the use language construct. From PHP 7.1,
these variables must not include superglobals, $this, or variables
with the same name as a parameter.
function orderdata($l_col,$l_dir,$l_data) {
usort($l_data, function($a, $b) use ($l_col) {
return $a[$l_col] <=> $b[$l_col];
});
return $l_data;
}
answered Nov 21 '18 at 13:37
dn Ferdn Fer
8531818
8531818
Great, thanks dn Fer!
– B. Swans
Nov 21 '18 at 13:58
add a comment |
Great, thanks dn Fer!
– B. Swans
Nov 21 '18 at 13:58
Great, thanks dn Fer!
– B. Swans
Nov 21 '18 at 13:58
Great, thanks dn Fer!
– B. Swans
Nov 21 '18 at 13:58
add a comment |
Try the following:
function orderdata($l_col,$l_dir,$l_data) {
usort($l_data, function($a, $b) use $l_col{
return $a[$l_col] <=> $b[$l_col];
});
return $l_data;
}
Note that $l_col
was inaccessible in your anonymous sorting function and you have to pass it explicitly to add it to the scope.
Great, thanks for the responses Nigel and Manoj! The use command was new to me, so i appreciate it.
– B. Swans
Nov 21 '18 at 13:37
1
This answer looks correct but it is not (it doesn't compile). Next time check the correctness of the answer and explain why your answer solves the OP's question. Read more about inheriting variables from the enclosing scope in anonymous functions.
– axiac
Nov 21 '18 at 13:40
add a comment |
Try the following:
function orderdata($l_col,$l_dir,$l_data) {
usort($l_data, function($a, $b) use $l_col{
return $a[$l_col] <=> $b[$l_col];
});
return $l_data;
}
Note that $l_col
was inaccessible in your anonymous sorting function and you have to pass it explicitly to add it to the scope.
Great, thanks for the responses Nigel and Manoj! The use command was new to me, so i appreciate it.
– B. Swans
Nov 21 '18 at 13:37
1
This answer looks correct but it is not (it doesn't compile). Next time check the correctness of the answer and explain why your answer solves the OP's question. Read more about inheriting variables from the enclosing scope in anonymous functions.
– axiac
Nov 21 '18 at 13:40
add a comment |
Try the following:
function orderdata($l_col,$l_dir,$l_data) {
usort($l_data, function($a, $b) use $l_col{
return $a[$l_col] <=> $b[$l_col];
});
return $l_data;
}
Note that $l_col
was inaccessible in your anonymous sorting function and you have to pass it explicitly to add it to the scope.
Try the following:
function orderdata($l_col,$l_dir,$l_data) {
usort($l_data, function($a, $b) use $l_col{
return $a[$l_col] <=> $b[$l_col];
});
return $l_data;
}
Note that $l_col
was inaccessible in your anonymous sorting function and you have to pass it explicitly to add it to the scope.
edited Nov 21 '18 at 13:38
Blackbam
5,121124475
5,121124475
answered Nov 21 '18 at 13:34
Manoj SinghManoj Singh
22016
22016
Great, thanks for the responses Nigel and Manoj! The use command was new to me, so i appreciate it.
– B. Swans
Nov 21 '18 at 13:37
1
This answer looks correct but it is not (it doesn't compile). Next time check the correctness of the answer and explain why your answer solves the OP's question. Read more about inheriting variables from the enclosing scope in anonymous functions.
– axiac
Nov 21 '18 at 13:40
add a comment |
Great, thanks for the responses Nigel and Manoj! The use command was new to me, so i appreciate it.
– B. Swans
Nov 21 '18 at 13:37
1
This answer looks correct but it is not (it doesn't compile). Next time check the correctness of the answer and explain why your answer solves the OP's question. Read more about inheriting variables from the enclosing scope in anonymous functions.
– axiac
Nov 21 '18 at 13:40
Great, thanks for the responses Nigel and Manoj! The use command was new to me, so i appreciate it.
– B. Swans
Nov 21 '18 at 13:37
Great, thanks for the responses Nigel and Manoj! The use command was new to me, so i appreciate it.
– B. Swans
Nov 21 '18 at 13:37
1
1
This answer looks correct but it is not (it doesn't compile). Next time check the correctness of the answer and explain why your answer solves the OP's question. Read more about inheriting variables from the enclosing scope in anonymous functions.
– axiac
Nov 21 '18 at 13:40
This answer looks correct but it is not (it doesn't compile). Next time check the correctness of the answer and explain why your answer solves the OP's question. Read more about inheriting variables from the enclosing scope in anonymous functions.
– axiac
Nov 21 '18 at 13:40
add a comment |
$l_col
isn't in scope in the closure, you need touse()
it. See Extending the scope of variables into anonymous functions in the suggested duplicate.– iainn
Nov 21 '18 at 13:33