Skip to content

DataOps Blog

Just another DataOps site

  • Home
  • certification
  • consultant
  • consulting
  • Contact Us!
  • courses
  • Intro to Laravel
  • tools
  • trainer
  • training

PHP | Difference between array_slice and array_splice

January 17, 2022
By Manish Kumar In PHP

PHP | Difference between array_slice and array_splice

Arrays in PHP are variables that can holds multiple values. There are many methods for accessing and manipulating arrays. The methods array_slice () and array_splice () are widely used methods for array manipulations.

The array.slice () removes items from the array and then return those removed items as an array whereas array. Splice () method is selected items from an array and then those elements as a new array object.

It looks first that both are same in working, but there are various differences between them which we are going to discuss in this particular blog, let’s start with major difference between array_slice () and array_splice ().

Difference between array_slice () and array_splice ()

Array_slice ()

The array.slice () removes items from the array and then return those removed items as an array.

  • This array method forms a new array after execution in a sub-array of a given array.
  • There are 3 parameter in this function, First array_name, ‘start’ indicates the starting index and ‘end’ indicates the ending index and after selected array name.

Syntax: – array_slice ($array_name, start, end);

  • The changes do not reflect in the original array, original array remain the same. The result has to be assigned to a new array variable.
  • The value come in new array with the values in the selected sub-array of the given array. The values in the range start to (end-1) will be selected.

Example:-

Output:-

Array
(
    [0] => bittu
    [1] => nilesh
    [2] => santosh
    [3] => amit
)

Array
(
    [2] => bittu
    [3] => nilesh
    [4] => santosh
    [5] => amit
)

Array
(
    [c] => bittu
    [d] => nilesh
    [e] => santosh
    [f] => amit
)

Array
(
    [3] => nilesh
    [4] => santosh
)

array_splice ()

The array. Splice () method is selected items from an array and then those elements as a new array object.

  • This method is used to remove an item from the given array and also used to add new element in array.
  • There are mainly 4 parameter in this function, first ‘array_name’, second is starting point, and 3 is end point and last is 2nd array_name which we wish to add in this array at the place of deleted values.

  • Syntax :- array_splice($array1, start, end,$array2).
    • The changes reflect in the original array, no need to assigned result to any other new variable.
    • The return value is an array containing the deleted element.

    Example:-

    Output:-

    Array
    (
        [0] => manish
        [1] => rakesh
        [2] => tittu
        [3] => rittu
    )
    

    Array
    (
        [0] => manish
        [1] => rakesh
        [2] => tittu
        [3] => rittu
    )
    

    Array
    (
        [0] => manish
        [1] => rakesh
        [2] => mj
        [3] => cull
        [4] => notly
    )

    Conclusion

    array_slice() and array_splice() are very useful methods that you must know in PHP, they sound pretty much similar, but they are completely different in the way they work. It is very good thing to know the exact working and their deffrences.


    Thank you for reading this article. I hope you found it useful.
    Keep Practising and exploring.

    composer css html javascript jquery Laravel laravel-form MySQL php
    Written by:

    Manish Kumar

    View All Posts

    Recent Posts

    • Kubernetes CKA Training Course & Official Master Certification Program
    • Elastic Training Course & Official Master Certification Program
    • Prometheus Training Course & Official Master Certification Program
    • AppDynamics Training Course & Official Master Certification Program
    • Dynatrace Training Course & Official Master Certification Program
    • Grafana Training Course & Official Master Certification Program
    • Newrelic Training Course & Official Master Certification Program
    • Splunk Training Course & Official Master Certification Program
    • Datadog Training Course & Official Master Certification Program
    • Zabbix Training Course & Official Master Certification Program
    • MLOps Training Course & Official Master Certification Program
    • DataOps Training Course & Official Master Certification Program
    • AiOps Training Course & Official Master Certification Program
    • Gitops Training Course & Official Master Certification Program
    • SRE Training Course & Official Master Certification Program
    • DevSecOps Training Course & Official Master Certification Program
    • DevOps Training Course & Official Master Certification Program
    • Best Blog website for Health and Fitness mymedicplus.com/blog
    • Top 5 Basic Programming Question for interview purpose in JS.
    • JAVA SCRIPT VARIABLE AND DATATYPE.

    Archives

    Categories

    Meta

    • Log in
    • Entries feed
    • Comments feed
    • WordPress.org
    • Free Video Tutorials
    • DevOps School
    • Best DevOps
    • scmGalaxy
    • Artificial Intelligence
    • DataOps
    • AIOps
    • GuruKul Galaxy
    • DevOps Consulting
    • DevOps Freelancers
    • DevOps Trainer
    • Free Ebooks
    • School for Debugger
    • Holiday Landmark
    • Surgery Planet
    • My Hospital Now
    • My Medic Plus
    • ProfessNow
    • Cotocus
    • Stocks Mantra
    • I Reviewed

    Proudly powered by WordPress | Theme: BusiCare by SpiceThemes