Jump to content

find a value in an array


mathias

Recommended Posts

Okey. I want to find the smallest value in my array and then write it on the screen wit echo. I thnik that i got the 'founding part' but the number does'nt been writet out on the screen. Help!

 

<?php
function hittaminstatal($j, $lista){
        $min = $j;
        for ($j + 1; $j < sizeof($lista); $j++) {
            if ($lista[$j] < $lista[$min]) {
                $min = $j;
            }
        }
        return $min;   
}  

    $lista = array(0 => 8, 23, 12, 4);
$lista = hittaminstatal($j, $lista);
echo $min;
?>

Link to comment
Share on other sites

First off you are setting $min inside the function - it is not defined outside the function. Plus, you define $lista as an array and then you redefine it as the value returned from the function.

 

Even still, your funtion doesn't work to return the min value.

Link to comment
Share on other sites

Your code wont work as you always have to set the min number. Try:

function getmin($nums)
{
    // set min number to first number in the array
    $min = $nums[0];

    // loop through the array
    foreach($nums as $num)
    {
        // check that the number is less than curret min number
        if($num <= $min)
        {
            // set min to current number
            $min = $num;
        }
    }

    // return minimum number
    return $min;
}

$arr = array(25, 60, 8, 1, 22);

echo getmin($arr);

Link to comment
Share on other sites

who says you can't define a variable inside a function?

mjdamato didn't say you couldn't.  He said the variable defined in the function isn't visible outside it.

 

Plus, you define $lista as an array and then you redefine it as the value returned from the function.

I do that frequently when I have to perform a sequence of conversions on the single value, so its perfectly valid to do so.  However, in this case it is a mistake (most likely) because the function is returning the index of the smallest value but without the array to use as a reference, the index isn't worth much.

 

@ the OP

<?php
function hittaminstatal($lista){
  $min = FALSE;
  if(!is_array($lista) || !count($lista)){
    return $min;
  }
  foreach($lista as $key => $val){
    if($min === FALSE){
      $min = $key;
    }else if($val < $lista[$min]){
      $min = $key;
    }
  }
  return $min;
}

$lista = array(8, 23, 12, 4);
$min = hittaminstatal($lista); // This $min is not the same as that in the function, google 'variable scope'
echo $min . ", " . $lista[$min];
?>

Link to comment
Share on other sites

note: over the last 10 years or so i have learned the most about PHP by trying to make things easier and finding ways to simplify things. trying to learn how to do things in a more difficult way seems to me to be counterproductive. instead of learning something new or useful, you seem to be going backwards, trying to make things difficult for the sake of "learning more." min and max are written; no need to learn how they do what they do. i can't imagine what's going to happen when you start to reverse-engineer the GD and curl functions... my 2 cents.

Link to comment
Share on other sites

Learning how things work under the hood simply opens up your options.  If you have two different functions that perform the same task but perform it differently and you understand the differences, you can better decide which one to call based on your current situation.  Likewise, when you are faced with a "special case" for which there is no built in function, understanding how others solved similar, more simple, problems can help you solve your own.  Lastly, writing your own functions to duplicate built in methods is a good way to learn about algorithm analysis and code efficiency.

 

There should come a time when one is familiar enough with such concepts that they use the built in methods and tools where available though.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.