Find which index to use in PHP array [duplicate]
up vote
-1
down vote
favorite
This question already has an answer here:
Elegant way to search an PHP array using a user-defined function
6 answers
binary search in array that contains range
1 answer
I have array like following one:
Array
(
[0] => stdClass Object
(
[lower_bond] => 1
[upper_bond] => 1
[money] => 13
)
[1] => stdClass Object
(
[lower_bond] => 2
[upper_bond] => 5
[money] => 8
)
[2] => stdClass Object
(
[lower_bond] => 6
[upper_bond] => 10
[money] => 3
)
[3] => stdClass Object
(
[lower_bond] => 11
[upper_bond] => 50
[money] => 1
)
)
Now I also have variable $rank.
so lets say if $rank=8
then I need to parse through above array and find where does the rank fits like following:
$rank>=lower_bond && $rank<=upper_bond
I need to get index in the array where above condition is true.
now I am doing this using foreach every time.
Is there any other way possible?
like can I use array_filter in my situation?
or any other ideas?
thanks in advance :)
php codeigniter
marked as duplicate by MonkeyZeus, trincot
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 19 at 20:13
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.
|
show 12 more comments
up vote
-1
down vote
favorite
This question already has an answer here:
Elegant way to search an PHP array using a user-defined function
6 answers
binary search in array that contains range
1 answer
I have array like following one:
Array
(
[0] => stdClass Object
(
[lower_bond] => 1
[upper_bond] => 1
[money] => 13
)
[1] => stdClass Object
(
[lower_bond] => 2
[upper_bond] => 5
[money] => 8
)
[2] => stdClass Object
(
[lower_bond] => 6
[upper_bond] => 10
[money] => 3
)
[3] => stdClass Object
(
[lower_bond] => 11
[upper_bond] => 50
[money] => 1
)
)
Now I also have variable $rank.
so lets say if $rank=8
then I need to parse through above array and find where does the rank fits like following:
$rank>=lower_bond && $rank<=upper_bond
I need to get index in the array where above condition is true.
now I am doing this using foreach every time.
Is there any other way possible?
like can I use array_filter in my situation?
or any other ideas?
thanks in advance :)
php codeigniter
marked as duplicate by MonkeyZeus, trincot
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 19 at 20:13
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.
2
What's wrong withforeach? It easily outperforms other solutions.
– trincot
Nov 19 at 20:11
Yes, you can usearray_filter()but it's just a loop under the hood anyways and provides no performance improvement. Can you specify why you wish to avoidforeach(){}?
– MonkeyZeus
Nov 19 at 20:11
The problem witharray_filteris that it still iterates the whole array, even there is a match at the very first entry.
– trincot
Nov 19 at 20:12
there could be 1000s of ranks i need to perform this and this is running under a for loop for each of those ranks .so i just wanted to avoid foreach to reduce cpu usage
– Desi Mulga
Nov 19 at 20:13
1
Iterating over array is always loop, always.
– u_mulder
Nov 19 at 20:14
|
show 12 more comments
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
This question already has an answer here:
Elegant way to search an PHP array using a user-defined function
6 answers
binary search in array that contains range
1 answer
I have array like following one:
Array
(
[0] => stdClass Object
(
[lower_bond] => 1
[upper_bond] => 1
[money] => 13
)
[1] => stdClass Object
(
[lower_bond] => 2
[upper_bond] => 5
[money] => 8
)
[2] => stdClass Object
(
[lower_bond] => 6
[upper_bond] => 10
[money] => 3
)
[3] => stdClass Object
(
[lower_bond] => 11
[upper_bond] => 50
[money] => 1
)
)
Now I also have variable $rank.
so lets say if $rank=8
then I need to parse through above array and find where does the rank fits like following:
$rank>=lower_bond && $rank<=upper_bond
I need to get index in the array where above condition is true.
now I am doing this using foreach every time.
Is there any other way possible?
like can I use array_filter in my situation?
or any other ideas?
thanks in advance :)
php codeigniter
This question already has an answer here:
Elegant way to search an PHP array using a user-defined function
6 answers
binary search in array that contains range
1 answer
I have array like following one:
Array
(
[0] => stdClass Object
(
[lower_bond] => 1
[upper_bond] => 1
[money] => 13
)
[1] => stdClass Object
(
[lower_bond] => 2
[upper_bond] => 5
[money] => 8
)
[2] => stdClass Object
(
[lower_bond] => 6
[upper_bond] => 10
[money] => 3
)
[3] => stdClass Object
(
[lower_bond] => 11
[upper_bond] => 50
[money] => 1
)
)
Now I also have variable $rank.
so lets say if $rank=8
then I need to parse through above array and find where does the rank fits like following:
$rank>=lower_bond && $rank<=upper_bond
I need to get index in the array where above condition is true.
now I am doing this using foreach every time.
Is there any other way possible?
like can I use array_filter in my situation?
or any other ideas?
thanks in advance :)
This question already has an answer here:
Elegant way to search an PHP array using a user-defined function
6 answers
binary search in array that contains range
1 answer
php codeigniter
php codeigniter
asked Nov 19 at 20:08
Desi Mulga
237
237
marked as duplicate by MonkeyZeus, trincot
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 19 at 20:13
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 MonkeyZeus, trincot
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 19 at 20:13
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.
2
What's wrong withforeach? It easily outperforms other solutions.
– trincot
Nov 19 at 20:11
Yes, you can usearray_filter()but it's just a loop under the hood anyways and provides no performance improvement. Can you specify why you wish to avoidforeach(){}?
– MonkeyZeus
Nov 19 at 20:11
The problem witharray_filteris that it still iterates the whole array, even there is a match at the very first entry.
– trincot
Nov 19 at 20:12
there could be 1000s of ranks i need to perform this and this is running under a for loop for each of those ranks .so i just wanted to avoid foreach to reduce cpu usage
– Desi Mulga
Nov 19 at 20:13
1
Iterating over array is always loop, always.
– u_mulder
Nov 19 at 20:14
|
show 12 more comments
2
What's wrong withforeach? It easily outperforms other solutions.
– trincot
Nov 19 at 20:11
Yes, you can usearray_filter()but it's just a loop under the hood anyways and provides no performance improvement. Can you specify why you wish to avoidforeach(){}?
– MonkeyZeus
Nov 19 at 20:11
The problem witharray_filteris that it still iterates the whole array, even there is a match at the very first entry.
– trincot
Nov 19 at 20:12
there could be 1000s of ranks i need to perform this and this is running under a for loop for each of those ranks .so i just wanted to avoid foreach to reduce cpu usage
– Desi Mulga
Nov 19 at 20:13
1
Iterating over array is always loop, always.
– u_mulder
Nov 19 at 20:14
2
2
What's wrong with
foreach? It easily outperforms other solutions.– trincot
Nov 19 at 20:11
What's wrong with
foreach? It easily outperforms other solutions.– trincot
Nov 19 at 20:11
Yes, you can use
array_filter() but it's just a loop under the hood anyways and provides no performance improvement. Can you specify why you wish to avoid foreach(){}?– MonkeyZeus
Nov 19 at 20:11
Yes, you can use
array_filter() but it's just a loop under the hood anyways and provides no performance improvement. Can you specify why you wish to avoid foreach(){}?– MonkeyZeus
Nov 19 at 20:11
The problem with
array_filter is that it still iterates the whole array, even there is a match at the very first entry.– trincot
Nov 19 at 20:12
The problem with
array_filter is that it still iterates the whole array, even there is a match at the very first entry.– trincot
Nov 19 at 20:12
there could be 1000s of ranks i need to perform this and this is running under a for loop for each of those ranks .so i just wanted to avoid foreach to reduce cpu usage
– Desi Mulga
Nov 19 at 20:13
there could be 1000s of ranks i need to perform this and this is running under a for loop for each of those ranks .so i just wanted to avoid foreach to reduce cpu usage
– Desi Mulga
Nov 19 at 20:13
1
1
Iterating over array is always loop, always.
– u_mulder
Nov 19 at 20:14
Iterating over array is always loop, always.
– u_mulder
Nov 19 at 20:14
|
show 12 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
2
What's wrong with
foreach? It easily outperforms other solutions.– trincot
Nov 19 at 20:11
Yes, you can use
array_filter()but it's just a loop under the hood anyways and provides no performance improvement. Can you specify why you wish to avoidforeach(){}?– MonkeyZeus
Nov 19 at 20:11
The problem with
array_filteris that it still iterates the whole array, even there is a match at the very first entry.– trincot
Nov 19 at 20:12
there could be 1000s of ranks i need to perform this and this is running under a for loop for each of those ranks .so i just wanted to avoid foreach to reduce cpu usage
– Desi Mulga
Nov 19 at 20:13
1
Iterating over array is always loop, always.
– u_mulder
Nov 19 at 20:14