PHP shorten array

Posted on 2005-04-18
Last Modified: 2008-02-26
I haven't found an array function yet that will shorten an array to the specified number of elements.  Anyone know what would help here, please?  Also, looking for a function to remove the first x number of elements from the beginning of an array.

Question by:earthman100
    LVL 8

    Accepted Solution

    I think you are looking for array_slice() ( ) To shorten an array to the first 24 elements use the following:

    $output = array_slice($input, 0, 24);

    or for the last 24:

    $output = array_slice($input, -24);

    Further, array_shift ( ) will remove the first element of an array. Also take a look at array_splice ( ), which gives you the possibility to replace parts of the array.

    Author Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to dynamically set the form action using jQuery.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now