PHP Coding Tips

Thursday, March 21, 2013

PHP: Get last element of array

Often you need to get the last element of PHP array. This is easy to achieve with using count() function, wich counts all elements in array. Because PHP arrays are indexed from 0, you must remeber to get "count()-1" element of an array, not "count()" element:$exampleArray = array( 3,6,8,11,14 ); $lastElement = $array[ count($exampleArray) - 1]; echo $lastElement; // prints 14This is the obvious way for some begginer developers. But more experienced developers will know, that there is a function defined especially for this purpose: echo end( $exampleArray ); // prints 14 Because this function will move internal array pointer to the end, you can reset it if you are currently iterating over this orray, for example: reset( $exampleArray ); // prints 14 If you are afraid about non set array, or set as other type than array, you can use function is_array() to check if this variable is defined as array. You can also get a last key of an array in several ways: echo end(array_keys( $exampleArray) ); // or: end( $exampleArray ); echo key( $exampleArray ); If you don't want to move internal array pointer, you can use another one-liner to get last element from array: $last = array_slice($array, -1, 1, true);

42 comments:

  1. Excellent post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
    My Sql Training Videos

    Tableau Training Videos

    ReplyDelete
  2. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    Application Packaging Training From India

    ORACLE Apps Technical Training From India

    ReplyDelete
  3. Excellent post !!!. The strategy you have posted on this technology helped me get into
    the next level and had lot of information in it.
    Sql server dba Online Training

    Sql Server Developer Online Training

    SAP PM Online Training

    SAP Hybris Online Training

    ReplyDelete
  4. QuickBooks Tech Support program is developed in such a fashion that it will supply you with the most effective account management mention of this era. However, you might face the issue along.

    ReplyDelete
  5. You will need to choose QuickBooks Enterprise Technical Support Number to cover your valuable time and cash on development of your organization in place of resolving errors in your accounting software. Even as we provide one day customer care at , your issues are resolved at any instance of times from technically skilled professionals at minimal price

    ReplyDelete
  6. QuickBooks Online Payroll Contact Number
    helps one to resolve all your valuable technical and functional problems whilst looking after this well known extensive, premium and end-to-end business accounting and payroll management software. Our experts team at QuickBooks payroll support number is going to make you understand its advanced functions and assists someone to lift up your business growth.

    ReplyDelete
  7. In accordance with statics released because of the Bing & Google search insights a lot more than 50,000 people who are seraching the web to get the QuickBooks Customer Service on a daily basis and more than 2,000 quarries related to Quickbooks issues and errors .

    ReplyDelete

  8. The QuickBooks 24/7 Payroll Support Phone Number USA team at site name is held accountable for removing the errors that pop up in this desirable software. We look after not letting any issue can be found in between your work and trouble you in undergoing your tasks.

    ReplyDelete
  9. With exceptional features, QuickBook helps all of the forms of businesses with generating accounting reports, entries for almost any sale, transactions pertaining to banking, etc., with a lot of ease. And along side support for QuickBooks Support Phone Number, it is less difficult to handle most of the tools of QuickBooks in a hassle-free manner.

    ReplyDelete
  10. QuickBooks Enterprise Support Number have got the most suitable choice for connecting your field staff and your in-house staff to be able to advertise closeness and harmony amongst both the groups.

    ReplyDelete
  11. We have the best in addition to the most convenient solution to increase your productivity by solving every issue you face while using the software. Contact us at QuickBooks Help & Support to avail the best customer care services made for you.

    ReplyDelete
  12. QuickBooks Customer Care Telephone Number: Readily Available For every QuickBooks Version
    Consist of a beautiful bunch of accounting versions, viz., QuickBooks Pro, QuickBooks Premier, QuickBooks Enterprise, QuickBooks POS, QuickBooks Mac, QuickBooks Windows, and QuickBooks Payroll, QuickBooks has grown to become a dependable accounting software that one may tailor depending on your industry prerequisite. As well as it, our
    QuickBooks Support Phone Number will bring in dedicated and diligent back-end helps for you for in case you find any inconveniences in operating any of these versions.

    ReplyDelete
  13. Amongst a number of these versions you may select the the one that suits your on line business the best. When you should really be realizing that QuickBooks has made bookkeeping an easy task, you'll find times when you may possibly face a few errors that may bog over the performance when it comes to business. QuickBooks Technical Support is the better location to look for instant help for virtually any QuickBooks related trouble.

    ReplyDelete
  14. You can use QuickBooks Support Phone Number to come up with any selection of reports you wish, keeping entries for several sales, banking transactions and plenty of additional.

    ReplyDelete
  15. It really is terribly frustrating, to say the smallest amount when you face one particular error. Errors hamper the task pace however additionally disturb your mental peace. Our QuickBooks Techical Support Number take all of the errors terribly seriously and they will fix all of the errors.

    ReplyDelete
  16. QuickBooks Tech Support Phone Number is accounting software, which will be a cloud-based application produced by Inuit Inc. As a matter of fact, the program happens to be developed with the intention of keeping a safe record of financial needs for the business. Additionally, it really is a user-friendly accounting software.

    ReplyDelete
  17. Installing of QuickBooks Pro is a seamless task by making use of QuickBooks Tech Support Phone Number It is rather possible that one could face trouble while installing QuickBooks Pro software since this one of the most common issue.

    ReplyDelete
  18. QuickBooks Support You are able to send the files directly without converting them into portable file documents. This software has turned out to be quite convincing and beneficiary for individuals who run their businesses.

    ReplyDelete
  19. QuickBooks Tech Support Number, which is a cloud-based application produced by Inuit Inc. In fact, the application has been developed with the intention of keeping a safe record of financial needs for the business. Additionally, it is a user-friendly accounting software; an easy task to maintain; assisting the company in keeping the records of financial transactions, and a whole lot more features.

    ReplyDelete
  20. And also with this, many QuickBooks users encounter some heavy and unexpected errors such as QuicKbooks Customer Support Phone Number and many more. So in such type of tough situations, you can rely on on-to the best QuickBooks customer support that you have to subscribe so that you can use our all amazing support services whenever you face error or any type of issue.

    ReplyDelete
  21. You can be confident; all the errors and problems are handled because of the simplest in operation. Our specialists can get to find on your drawback at once. this can be the reason we have a tendency to square measure recognized for our client Support services. we now have a tendency to rank our customers over something and that we make an effort to offer you a swish accounting and management expertise. you’ll additionally visit our web site to induce to understand additional concerning our code and its own upgrades. you’ll scan in-depth articles concerning most of the errors and additionally the best way to resolve them. Rectifying errors desires in-depth information about the system and its intricacies. Our internet site may be a go-to supply for everything connected with QuickBooks Support.

    ReplyDelete
  22. Now You are well conscious of advantages of QuickBooks Payroll Technical Support Number in your company accounting but since this premium software contains enhanced functions to help you with your accounting task to accomplish, so you might face some technical errors while using QuickBooks payroll solution.

    ReplyDelete
  23. QuickBooks Tech Support Phone Number is a site provided by Intuit which lets you use Payroll along with your QuickBooks Software. Enhanced Payroll and Full-service payroll come in the Online Payroll whereas Basic, Enhanced and Assisted Payroll come under Payroll for Desktop.

    ReplyDelete

  24. Any QuickBooks Tech Support Phone Number user faces any sort of identified errors in their daily accounting routine; these errors may differ from 1 another to a large degree, so our dedicated QuickBooks Payroll Pro-Advisers are very well loaded with their tools and expertise to give most effective resolutions very quickly to the customers.

    ReplyDelete
  25. It’s great to come across a blog every once in a while that isn’t the same out of date rehashed material. Fantastic read.
    Datascience Course in Chennai | Datascience Training in Chennai

    ReplyDelete
  26. Quickbooks software is designed for the best account management experience of this era. If you may face the problem with your Quickbooks Software and start searching for the solution. No need to worry if you are facing trouble with your software you just away from your solution. If you would like to learn how to Troubleshoot Quickbooks Error 9999, you can continue reading this blog.

    ReplyDelete
  27. This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic...
    Java Training in Chennai

    Java Training in Velachery

    Java Training in Tambaram

    Java Training in Porur

    Java Training in Omr

    Java Training in Annanagar

    ReplyDelete