i have array with > 1000 values, array is simple

if i want to get max value i must to check all values in array in foreach or while

but how can i get max value from array like in sql

select max from array ,

or for example if i have associative array.

select max value from array where array[KEY]='some value'

something of this is possible with arrays or associative arrays ?

    Im not to sure if there is one, but you could sort the array and then pop the last element off.

      Sorting the array and using array_pop() is silly, we discussed that before...

      Actually, you have this associative array, some elements of which have the same key?
      Then from among these, you want to find the highest value?

      Perhaps 1 simple way would be to sequentially place all elements that have that particular key into another array, then use max() on that new array.

      Conversely, you could use:

      $searchkey = 'somekey';
      $maxval = $array[$searchkey];
      foreach ($array as $key => $val) {
      	if ($key == $searchkey) {
      		if ($val > $maxval) {
      			$maxval = $val;
      		}
      	}
      }

      then $maxval will contain the value required

        sorry peoples i dont ask you , how to get a max value from array.

        my question was " is something similar to structured query in work with arrays"

        but also thanks for your replies.....

          @ laserlight

          Sorting the array and using array_pop() is silly, we discussed that before...

          LOL
          were the hell have we discussed this befor... and please explain to me why it would be silly, while we are on this subject.

            um, you were asking about obtaining the largest value in an array.
            you mentioned checking all values in the array, and indeed you have to do it.
            there simply is no method for retrieving the largest value in the array, with or without the given parameters, without searching the whole array.
            mysql does it, just that you dont see the code.
            of course, if the array is sorted to begin with then indeed one simply takes the value as if it were random access, but other than that one doesnt have random access to such values.
            you could create a class or something that does have random access to the max and min values of an array, but then the creation and updating of the array would take longer.

            I thought that in particular you wanted to emulate a mysql query like
            SELECT MAX(some_value) AS some_value FROM some_table WHERE some_key='mykey';

            Oh, JakesSA, this was the thread:
            http://www.phpbuilder.com/board/showthread.php?s=&threadid=10237448

              re>I thought that in particular you wanted to emulate a mysql query like

              yes 🙂

                oh yes laserlight ... where the hell do you see my name on that thread ... next time, get your facts straight before you tell me that WE have discussed a topic before.

                  oh, when I wrote 'we' I meant the members of this forum.

                    Write a Reply...