Case study: PHP array query _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Case study: PHP array query. When learning PHP arrays, you may encounter problems. here we will introduce PHP array queries and share them with you here. PHP4.0 contains more than 30 new array-related functions. When learning PHP arrays, you may encounter problems. here we will introduce PHP array queries and share them with you. PHP4.0 contains more than 30 new array-related functions. Many common functions allow you to check for specific objects in a given array, count array elements, add or delete elements, or sort elements.

If you have a large array and all you need to do is find an existing given value, you can use in_array () to return true or false. The following code outputs "Not found in this array" -- because you will find an existing "Alber" in $ namesArray ". You can add elements to any array, whether at the beginning or end of an existing array. You can also use a function to create a new array containing two or more array elements. During merging, each array is arranged in the required order. If your array already has internal sorting, you need to re-sort the new merged array.

PHP array query code:

 
  1. /**
  2. * Inverted array
  3. * @ ParamaInputarray the array to be sorted
  4. * @ Param $ bystring: sorting objects, keys are sorted by array subscript, and values are 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, $ );
  14. Print_r ($ tmMax );
  15. List ($ k, $ v) = each ($ tmMax );
  16. $ Res [$ k] = $ v;
  17. }
  18. Return $ res;
  19. }
  20. /**
  21. * @ ParamaInputarray array
  22. * @ Param $ bystring: sorting objects, keys are sorted by array subscript, and values are sorted by value
  23. */
  24. FunctiongetMax (& $ aInput, $ ){
  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' =>300, 'Orange '=>150, 'bana' =>100, 'Mango' =>330 );
  42. Echo'<Xmp>';
  43. Print_r (aryReverts ($ list, 'value '));

Bytes. PHP4.0 contains 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.