Jump to content

[SOLVED] array copy


pkedpker

Recommended Posts

:D lol I know this is stupid question but here is what I want to do.. instead of making many arrays I want to access 1 array and get parts of it's elements in all the sub arrays for randomizing them..

 

so I got

 

$gs = array(1,4,7,10,13,16,19,21,23,27,29,32,37,41,43,46,48,50,52);

 

now I want

 

$ks[] = $gs[6, 12];

 

so it would copy from 6th index to 12th. and store the 6 elements in ks array.

 

but how do I do that without making a multi-dimensional array as i did. I seen array_slice but it cuts of the original array which is a not and i dont want to make clone of master array just to slice it.

Link to comment
Share on other sites

array_slice does not overwrite the existing array, unless you assign the results of array_slice to the same array.

 

$gs = array(1,4,7,10,13,16,19,21,23,27,29,32,37,41,43,46,48,50,52);
$ks = array_slice($gs,5,6);
echo "<pre>";
print_r($gs);
print_r($ks);

 

output:

Array
(
    [0] => 1
    [1] => 4
    [2] => 7
    [3] => 10
    [4] => 13
    [5] => 16
    [6] => 19
    [7] => 21
    [8] => 23
    [9] => 27
    [10] => 29
    [11] => 32
    [12] => 37
    [13] => 41
    [14] => 43
    [15] => 46
    [16] => 48
    [17] => 50
    [18] => 52
)
Array
(
    [0] => 16
    [1] => 19
    [2] => 21
    [3] => 23
    [4] => 27
    [5] => 29
)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.