Pavithra Gunasekara
Pavithra Gunasekara

Reputation: 4112

passing array as a function argument php

I have a problem with the following code, I need to pass an array as a function argument. I have 2 arrays and I need away to pass them to the function. something is wrong with the code and I am not that much familiar with php also. Can anyone please help me with this. Thanks a lot...

here's the php code

$array1=array('e11','e12','e13','e14','e15');
$array2=array('e21','e22','e23','e24','e25');

function randomSort($arr){

    for ($i=0; $i<2; $i++) {

        $random = array_rand($arr);  # one random array element number
        $get_it = $arr[$random];    # get the letter from the array
        echo $get_it;   
        unset($arr[$random]);
     }
}
randomSort($array1);
randomSort($array2);

Upvotes: 1

Views: 7307

Answers (1)

Gaurav
Gaurav

Reputation: 28755

$array1=array('e11','e12','e13','e14','e15');
$array2=array('e21','e22','e23','e24','e25');

function randomSort(&$arr){  // pass array by reference

    for ($i=0; $i<2; $i++) {

        $random = array_rand($arr);  # one random array element number
        $get_it = $arr[$random];    # get the letter from the array
        echo $get_it;   
        unset($arr[$random]);
        $arr =  array_values($arr);
     }
}
randomSort($array1);
randomSort($array2);

Upvotes: 2

Related Questions