Array_splice function Result Assignment

Source: Internet
Author: User
$arr 1 = Array (All-in-all);
$arr 2 = array (3,4,5);
$arr 1 = array_splice ($arr 2,1,1);
Print_r ($arr 1);


The result is an array ([0] = 4)
In a logical sense, the assignment operator does not take place until the Array_splice function is finally completed.
Then Array_splice ($arr 2,1,1) Gets the value within the array $arr2 should be (3,4),
Then the operation "=", then get the contents of the $ARR1 array should be (3,4), but actually run the entire
$arr 1 = array_splice ($arr 2,1,1), but the contents of the $ARR1 array are (4), solved.


Reply to discussion (solution)

Why, what's your account?
Array_splice ($arr 2,1,1) removes 1 elements from the 1th element of $arr 2 (count starting from 0) and returns the deleted element
You only let him delete 1, he will not be the one who has to cut 2.

The Array_slice () function takes a value out of the array and returns it as a condition.

You don't understand the meaning of this function ... You write code that means, starting at the second position of the array, taking a value

Upstairs, my intention is to use Array_splice to remove the second bit of $ARR2 and assign it to $ARR1, then display the values in the remaining array, and then write the code in handy: $arr 1 = array_splice ($arr 2,1,1).
I do not understand the results, I think the online definition of array_splice is to delete or replace the value of the specified part of the array, but the result of $arr1 = Array_splice ($arr 2,1,1) is the value I want to delete, whether $ARR1 = array _splice ($arr 2,1,1) equivalent to Array_slice ($arr 2,1,1)?

The number in the handbook is clear: Returns an array containing the removed cells
Array_splice ($arr 2,1,1) is equivalent to Array_slice ($arr 2,1,1) only from the returned results
But the former has changed the original state of $arr 2.

Thank you, 5 floor. The return value of this function is not understood in the information book, but it is always assumed that this function returns the remaining data from 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.