I am working on a PHP-based web-application. The version of PHP running is 5.2.x! I have a function play
in the program, as well as an array called $study
, defined outside play
, containing a few elements.
function play( $games, $time ) {
//Do something here
$lol = array_search(strtolower($games), array_map('strtolower', $study['somevar']));
}
Now the point here is I am calling play()
elsewhere in my code but as you can see I reference $study['somevar']
but the function takes only two arguments: $games
& $time
so there is no way to reference $study['somevar']. I know sometimes in PHP this also works
function play($games, $time) use ($study){
//do something
}
But when i try to run using this use
syntax in PHP 5.2 than it gives error. So, How can I possibly do this? Any ideas?
Edit 1:
So, this is what Array of $study prints out (which i tried outside function)
echo $study['somevar'] : It prints out Array (English word "Array")
echo $study['somevar'][0] : It prints out blahblah (0th element)
echo $study['somevar'][1] : It prints out OKoK (1st element)
In simple words , by using var_dump($study) i got
["somevar"]=>
array(2) {
[0]=>
string(8) "blahblah"
[1]=>
string(4) "OKoK"
There is no 2nd Element in study -> somevar .. So, only two (0th and 1st)
Now, basically I want to use this Array to search it with $games inside the function play
. So, for that inside the function; as you can see above in pseudo code i have used
array_search(strtolower($games), a second element)
Now this Second element inside array_search, i want all the elements of the array $study (i.e blahblah, OKoK) to be converted to lower case, so that i could match easily using array_search
method. Hence, i used the logic to array_map('strtolower', $study['somevar'])
..
and placed it into array_search
method like :
$lol = array_search(strtolower($games), array_map('strtolower', $study['somevar']));
Now, In further code i pass some values in function play('OkOk', 10);
and in this the OKOK should match into our hard-coded $study array as the element 1 contains that key . This is the logic. But i get error like
array_map() Argument #2 should be an array in ..........
array_search() [<a href='function.array-search'>function.array-search</a>]: Wrong datatype for second argument in .........
You can't use use
in PHP 5.2 because it was introduced in PHP 5.3 but you can use global
:
function play( $games, $time ) {
global $study;
//Do something here
$lol = array_search(strtolower($games), array_map('strtolower', $study['somevar']));
}