Strange PHP array problems

Source: Internet
Author: User
The strange problem with the PHP Array is as follows: PHPcode $ arr1 contains the data obtained from the database, and print_r is as follows: Array ([0] = & gt; 30544 [1] = & gt; 30544 [2] = & gt; 30532 [3] Strange PHP array problems
As follows:
PHP code
  $ Arr1 contains the data retrieved from the database, and print_r is as follows: array ([0] => 30544 [1] => 30544 [2] => 30532 [3] => 30550 [4] => 30544 [5] => 30544 [6] => 30532 [7] => 30532 [8] => 30532 [9] => 30550 [10] => 30544 [11] => 30544 [12] => 30544 [13] => 30544 [14] => 30544 [15] => 30544 [16] => 30532 [17] => 30532 [18] => 30532 [19] => 30532) $ arr2 = array ('1', '2', '4', '5'); // I use the same function max () to obtain the maximum value. The result is as follows: // An Error is returned for the result of arr1, but the data 30550A PHP Error was encounteredSeverity: WarningMessage: max () [function. max]: When only one parameter is given, it must be an arrayFilename: test. phpLine Number: 104 // arr2 normal, output 5


The above shows why the error is reported. how can this problem be solved. Thank you.


------ Solution --------------------
Max () [function. max]: When only one parameter is given, it must be an array

When max is a parameter, it must be an array.
PHP code
LogminMath function PHP Manual ------------------------------------------ max (PHP 4, PHP 5) max-find the maximum value description mixed max (number $ arg1, number $ arg2) mixed max (array $ numbers [, array $...]) max () returns the maximum value in the parameter. If there is only one parameter and it is an array, max () returns the maximum value of this array. If the first parameter is an integer, string, or floating point number, at least two parameters are required and max () returns the largest of these values. You can compare an infinite number of values. Note: PHP regards a non-numeric string as 0, but if this is the largest value, it returns a string. If multiple parameters are evaluated as 0 and the value is the maximum value, max () returns the value 0. if the parameter does not contain the value 0, returns the string with the largest alphabetic order. Example #1 use max ()
  See min () and count (). User Contributed Notessun at drupal dot org 03-Aug-2011 :25 Note that max () throws a warning if the array is empty:
  So make sure your data isn't empty. php at rijkvanwel dot nl 12-Apr-2011 To get the largest key in an array:
  'First', 1 => 'second ',/*... */99 => 'Nth'); $ max_key = max (array_keys ($ array); // 99?> Alex Stanhope 28-Oct-2010 If you want to test whether x lies within two bounds:
  = Min ($ y1, $ y2) & ($ x <= max ($ y1, $ y2);} // called by: class :: isInRange (10, 12, 2); // or $ this-> isInRange (10, 12, 2); // or (drop static) isInRange (10, 12, 2 ); // simple test function: static function unit_isInRange () {$ output = array (); $ inputlist [] = array (10, 12, 2, true ); $ inputlist [] = array (13, 12, 2, false); $ inputlist [] = array (2,-2, 2, true ); $ inputlist [] = array (2,-8,-2, false); foreach ( $ Inputlist as $ input) {$ output [] = array ('input' => array ($ input [0], $ input [1], $ input [2]), 'output' => self: isInRange ($ input [0], $ input [1], $ input [2]), 'pected' => $ input [3],);} return ($ output) ;}?> Dan at coders dot co dot nz 22-May-2010 max () on undefined parameters does not assume the value is zero, it ignores it.
  -2); // If $ dimensions ['right'] was never set, // we may expect CT it to be treated as zero, but print max ($ dimensions ['left'], $ dimensions ['right']); // Returns-2, not zero print max (0 + $ dimensions ['left'], 0 + $ dimensions ['right']);?> Wocould be a work-around UND, but it's probably tidier to ensure your values are set correctly first. (on PHP 5.2.11 anyway) Alex Rath 10-Apr-2010 11: 27 Notice that whenever there is a Number in front of the String, it will be used for Comparison.
  But just if it is in front of the String
  Grillen at abendstille dot at 02-Apr-2010 0:55 max only accepts not empty arrays. if you work a lot with numerical arrays and with max, this function may come in handy:
  Artem dot yagutyan at gmail dot com 27-Mar-2010 0:28 This Is Good Example: For max to min
  $ Val) {if ($ val = max ($ array) return $ key ;}$ array = array (10, 2, 5, 7, 4, 15, 32, 8, 425); $ array_count = count ($ array); for ($ I = 1; $ I <= $ array_count; $ I ++) {$ max_val [$ I] = max_key ($ array); $ view = $ array [$ max_val [$ I]; unset ($ array [$ max_val [$ I]); print $ view."
"; //}/* OUTPUT41 // max3225151097542 // Min */?> Marcini 11-May-2009 Note that max () can compare dates, so if you write something like this: You will get: 2009-03-15. ries at vantwisk dot nl 09-Nov-2008 06:36 I had several occasions that using max is a lot slower then using a if/then/else construct. be sure to check this in your routines! Ries Marcus Zacco 29-Sep-2008 09:47 This code loops through seven arrays and finds the highest average value within those arrays-and changes the font color for it. great for highlighting. the biggest take-away is this the row if ($ average [$ I] = max ($ average) The number_format just rounds the numbers to 0 decimal points. ";} Else {echo" Value ". $ num.": ". number_format ($ average [$ I], 0, '.',''). "%
";}}?> ### OUTPUT Value 1: 52% Value 2: 58% Value 3: 56% Value 4: 73% Value 5: 77% <-this 77 is highlighted in red Value 6: 71% Value 7: 75% harmor 21-Feb-2008 A way to bound a integer between two values is: Which is the same: $ Max) {$ tmp = $ max;} $ y = $ tmp;?> So if you wanted to bound an integer between 1 and 12 for example: Input: '; $ X = 1; echo bound ($ x, 1, 12 ).'
'; $ X = 6; echo bound ($ x, 1, 12 ).'
'; $ X = 12; echo bound ($ x, 1, 12 ).'
'; $ X = 13; echo bound ($ x, 1, 12 ).'
';?> Output: 1 1 6 12 12 michaelangel0 at mail.com 04-Jul-2007 0:00 Matlab users and others may feel lonely without the double argument output from min and max functions. to have the INDEX of the highest value in an array, as well as the value itself, use the following, or a derivative: $ Maxvalue, "I" =>$ maxindex) ;}?> Jeremi23 at gmail dot com 14-Jun-2007 max on a an array with key/values 5, 2 => 3); echo max ($ tmp);?> This return 5, so the max is done on the values. johnmott59 at hotmail dot com 17-May-2007 To find the maximum value from a set of 1-dimen1_arrays, do this: The inner max () functions operate on the arrays, the outer max compares the numeric results of the inner ones. johnphayes at gmail dot com 02-May-2006 Regarding boolean parameters in min () and max () :( a) If any of your parameters is boolean, max and min will cast the rest of them to boolean to do the comparison. (B) true> false (c) However, max and min will return the actual parameter value that wins the comparison (not the cast ). here's some test cases to explain strate: 1. max (true, 100) = true2. max (true, 0) = true3. max (100, true) = 1004. max (false, 100) = 1005. max (100, false) = 1006. min (true, 100) = true7. min (true, 0) = 08. min (100, true) = 1009. min (false, 100) = false10. min (100, false) = false11. min (true, false) = false12. max (true, false) = true -------------------------------------------- logminMath function PHP Manual

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.