Hi people I need your help. Let's say I have an array.
$arr = array(
1 => array(
key1 => something,
key2 => something,
key3 => something,
testkey => 3,
keyx...),
2 => array(
key1 => something,
key2 => something,
key3 => something,
testkey => 0,
keyx...),
3 => array(
key1 => something,
key2 => something,
key3 => something,
testkey => 2,
keyx...),
4 => array(
key1 => something,
key2 => something,
key3 => something,
testkey => 2,
keyx...),
5 => array(
key1 => something,
key2 => something,
key3 => something,
testkey => 3,
keyx...),
6 => array(
key1 => something,
key2 => something,
key3 => something,
testkey => 0,
keyx),
7 => array(
key1 => something,
key2 => something,
key3 => something,
testkey => 3,
keyx...),
n =>array(blabla)
)
I can't know how many keys $arr has.
I need to test if one or more value of testkey are equal, if they are put this result on a variable.
2 and 6 are not considered since the testkey is 0.
(0 is my default value, i can put it to NULL)
3 and 4 have the value of 1 (first to match).
1, 5 and 7 have the value of 2 (second to match).
Then I have to store this values somehow:
$matched = array();
$matched[1] = array (2, 6);
$matched[2] = array (1, 5, 7);
But I'm sure it can be done better.
Thank you.
I would sort the array $arr
by the testkey
values. This can be done in O(n lg n) time. Then you iterate through the sorted array and adds the original keys to $matched[1]
. When the testkey
value changes, you start adding the next key values to $matched[2]
, etc.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.