Using array_slice() to extract part of an array

Discussion in 'Scripts' started by beingchinmay, Jul 29, 2016.

  1. beingchinmay

    beingchinmay ECZ Member

    Jan 1, 2016
    Likes Received:
    The standard pattern for array_slice() is as follows:

    The second argument (startIndex) indicates the position of the first element you want to extract to the new array. If the value of startIndex is negative, the position is counted from the end of the original array. If the third argument (length) is omitted, everything to the end of the original array is included in the new one. So far, both PHP and ActionScript act in exactly the same way, as the following examples show:

    If you test that code in a browser (it’s in array_slice1.php in the download files for this chapter), you will get the output shown at the right. The two new arrays ($goodguys and $goodguys2) produce exactly the same result, although the first counts from the beginning of the array, and the second counts from the end of the array. As you can see, the original array ($characters) remains intact.

    The following code produces a similar result in ActionScript (the code to display the result has been omitted, although it’s included in array_slice1.fla,or you can use the debugger to view the three arrays if you’re creating your own version):