user3854482 user3854482 - 1 month ago 13
HTML Question

Is there a way I could pass more than a limited amount of arrays through a php function?

I have this function which basically passes different arrays but it can only pass arrays with a length of 5. I was wondering if there is a way to pass various array lengths through this function?

The output needs to only show one item at a time rather than all. I have 8 arrays with various lengths which is why I am making one general function to pass the arrays through.

function myFunction(&$array){
//shuffling array contents
shuffle($array);

//isolate an array
if(in_array($array[0], $array)){
echo $array[0];
}
elseif(in_array($array[1], $array)){
echo $array[1];
}
elseif(in_array($array[2], $array)){
echo $array[2];
}
elseif(in_array($array[3], $array)){
echo $array[3];
}
elseif(in_array($array[4], $array)){
echo $array[4];
}
}

Answer

I feel like I'm probably still missing something, but it looks like this could be simplified to:

function myFunction(&$array){
    //shuffling array contents
    shuffle($array);

    //isolate an array
    echo isset($array[0]) ? $array[0] : '';
}

Which would take arrays of any length (including zero), shuffle and echo one item.

Comments