PHP spaceship sort not working with variable [duplicate]












0
















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?










share|improve this question















marked as duplicate by Nigel Ren php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

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 to use() it. See Extending the scope of variables into anonymous functions in the suggested duplicate.

    – iainn
    Nov 21 '18 at 13:33
















0
















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?










share|improve this question















marked as duplicate by Nigel Ren php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

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 to use() it. See Extending the scope of variables into anonymous functions in the suggested duplicate.

    – iainn
    Nov 21 '18 at 13:33














0












0








0









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?










share|improve this question

















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

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 php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

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 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

















$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












2 Answers
2






active

oldest

votes


















1














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;
}





share|improve this answer
























  • Great, thanks dn Fer!

    – B. Swans
    Nov 21 '18 at 13:58



















0














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.






share|improve this answer


























  • 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


















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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;
}





share|improve this answer
























  • Great, thanks dn Fer!

    – B. Swans
    Nov 21 '18 at 13:58
















1














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;
}





share|improve this answer
























  • Great, thanks dn Fer!

    – B. Swans
    Nov 21 '18 at 13:58














1












1








1







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;
}





share|improve this answer













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;
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 13:37









dn Ferdn Fer

8531818




8531818













  • 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





Great, thanks dn Fer!

– B. Swans
Nov 21 '18 at 13:58













0














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.






share|improve this answer


























  • 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
















0














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.






share|improve this answer


























  • 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














0












0








0







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.






share|improve this answer















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.







share|improve this answer














share|improve this answer



share|improve this answer








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



















  • 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



Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?