1

I have a multi-dim array to search:

> Array (
>     [91] => Array
>         (
>             [FoSW] => 117
>             [DfLR] => 107
>             [FoA] => 0
>             [SoG] => 116
>             [RST] => 38
>             [SSW] => 0
>             [total] => 458
>         )
> 
>     [92] => Array
>         (
>             [FoSW] => 118
>             [DfLR] => 0
>             [FoA] => 58
>             [SoG] => 0
>             [RST] => 0
>             [SSW] => 40
>             [total] => 463
>         ) //etc....

I am using this function which searches for the value and returns its key. IF the value is not found then it reduces the value by one and searches again.

$search is the md array

$type would be SoG for example (a key in the second level of the md array)

$score would be value 0f 24 for example

NOTE: in the entire array there is no SoG with a value of 24

function find_percentile($search, $type, $score){

    foreach($search as $key=>$val){

        if ($val[$type]== $score){
            return $key;
            $found = true;
        }
        else {
            $found = false;
        }
    }
    if ($found == false){
        $new_score = $score - 1;
        find_percentile($search, $type, $new_score);
    }
}

if I echo $key from the foreach loop it will eventually echo out the recursively found key.

but when i build a separate array with the function or echo the function it does not get the key found by recursion.

$perc_array[$key] = find_percentile($percentiles, $key, $val); 

this will only attach the matched values. Recursively found values will not be in this array.

echo find_percentile($percentiles, $key, $val);

will not echo recursively found values.

Smith Smithy
  • 585
  • 6
  • 24
  • Why don't you just use array_search()? – XaxD Aug 01 '13 at 17:19
  • it is possible that FoA and SoG have a value of 24 – Smith Smithy Aug 01 '13 at 17:21
  • if you use array_search, you can just `array_search($score, $search)` and you don't need to do any recursion.. you can just iterate through each 2nd dimension.. – XaxD Aug 01 '13 at 17:22
  • i am searching through all 100 master keys. each subset of keys repeats for every master key. [100][sog] = 1, [99][sog] = 2 etc... I am looking for which master key has [sog]=24 and if none found go to [sog]=23 and so on... – Smith Smithy Aug 01 '13 at 17:24

2 Answers2

2

You forgot to return the value, returned by the function:

if ($found == false){
    $new_score = $score - 1;
    return find_percentile($search, $type, $new_score);//<- here
}
user4035
  • 22,508
  • 11
  • 59
  • 94
  • REALLY??? YOU HAVE NO IDEA HOW RELIEVED I AM NOW. but, i thought that it would call the function again until the found value was returned in the if statement... I am a little confused. – Smith Smithy Aug 01 '13 at 17:37
  • @SmithSmithy "i thought that it would call the function again until the found value was returned in the if statement" - it would call the function, but it's value wouldn't be returned. Try to imitate, how computer works, carefully writing all the steps on paper. – user4035 Aug 01 '13 at 17:47
0
function find_percentile($search, $type, $score){
    $max = null;
    $where = 0;
    foreach($search as $key=>$val){
         if ($val[$type]== $score){
              return $key;
         } else if($val[$type]>$max && $val[$type] < $score) {
              $where = $key;
              $max = $val[$type];
         }
    }
    return $where;
}
ProfMax
  • 129
  • 3