Array_splice function result assignment

Source: Internet
Author: User
The result of the array_splice function is assigned $ arr1 = array (1, 2, 3 );
$ Arr2 = array (3, 4, 5 );
$ Arr1 = array_splice ($ arr2, 1, 1 );
Print_r ($ arr1 );


The result is Array ([0] => 4)
It is reasonable to say that the value assignment operator is executed only after the array_splice function is completed,
Then, the value in the array $ arr2 obtained by array_splice ($ arr2,) should be (3, 4 ),
Then calculate "=", and the content of the $ arr1 array should be (3, 4), but the entire
$ Arr1 = array_splice ($ arr2, 1, 1), but the content of the $ arr1 array is (4.


Reply to discussion (solution)

Why? What is your account?
Array_splice ($ arr2, 1st) deletes one element from the element of $ arr2 (counting starts from 0) and returns the deleted element.
You only want him to delete one, and he will never delete two

The array_slice () function extracts a value from the array based on the conditions and returns it.

You have not understood the meaning of this function ...... The code you write means that you can get a value from the second position of the array.

I want to use array_splice to delete the second digit of $ arr2 and assign it to $ arr1, and then display the values in the remaining array, then the code is written as $ arr1 = array_splice ($ arr2 ).
I don't understand the result. I think the definition of array_splice on the internet is to delete or replace the value of the specified part in the array, but $ arr1 = array_splice ($ arr2) the result of the code is the value I want to delete. is $ arr1 = array_splice ($ arr2, 1, 1) Equivalent to array_slice ($ arr2, 1, 1 )?

The number in the manual is clear: returns an array containing the removed unit
From the returned results, array_splice ($ arr2,) is equivalent to array_slice ($ arr2)
But the former has changed the original state of $ arr2.

Thank you on the 5th floor. I didn't understand the return value of this function in the document. I always thought that this function would return the remaining data of the original array. thank you.

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.