Case study PHP array query _php tutorial

Source: Internet
Author: User
When learning PHP arrays, you may encounter problems, here will introduce the PHP array query, here to take out and share with you. PHP4.0 has more than 30 new array-related functions. Many of these general functions allow you to check for specific objects in a given array, count arrays of elements, add or delete elements, or sort elements.

If you have a large array, and all you have to do is find a given value that exists, you can use In_array () to return True or false. The following code will output "not found the This array"-because you will find a "Alber" in $namesarray that does not exist. You can add elements to any array, either at the beginning or the end of an existing array. You can also use a function to create a new array that contains two or more arrays of elements. When merging, each array is arranged in the order in which it is needed. If your array already has an internal sort, you need to reorder the new merged array.

PHP Array Query Code:

 
 
  1. /**
  2. * Reverse Array
  3. * @paramaInputarray the array to sort
  4. * @param $bystring Sort objects, key is sorted by array subscript, value is sorted by value
  5. */
  6. Functionaryreverts ($aInput, $ by=' key ') {
  7. if (!is_array ($aInput)) {
  8. Returnfalse;
  9. }
  10. $ Res = Array ();
  11. $ N = Count ($aInput);
  12. For ($i=0; $i<$n; $i + +) {
  13. $ Tmmax = Getmax ($aInput, $by);
  14. Print_r ($tmMax);
  15. List ($k, $v) =each ($tmMax);
  16. $res [$k]= $v;
  17. }
  18. Return$res;
  19. }
  20. /**
  21. * @paramaInputarray the array to ditch
  22. * @param $bystring Sort objects, key is sorted by array subscript, value is sorted by value
  23. */
  24. Functiongetmax (& $aInput, $by) {
  25. $ Max = NULL ;
  26. $ Maxkey = NULL ;
  27. if ($by= = ' key ')
  28. $ Tar = ' K ' ;
  29. Else
  30. $ Tar = ' V ' ;
  31. foreach ($aInputas $k=>$v) {
  32. if ($ $tar>$max) {
  33. $ Max =$ $tar;
  34. $ Maxkey = $k;
  35. }
  36. }
  37. $ MaxValue = $aInput [$maxKey];
  38. Unset ($aInput [$maxKey]);
  39. ReturnArray ($maxkey=>$maxValue);
  40. }
  41. $ List = Array (' apple ' =>, ' orange ' =>, ' banana ' => , ' mango ' =>330);
  42. Echo ' <XMP> ';
  43. Print_r (Aryreverts ($list, ' value '));

http://www.bkjia.com/PHPjc/446541.html www.bkjia.com true http://www.bkjia.com/PHPjc/446541.html techarticle When learning PHP arrays, you may encounter problems, here will introduce the PHP array query, here to take out and share with you. PHP4.0 has more than 30 new array-related functions. Where ...

  • 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.