You're asking about a specific issue - which is simple to solve - but really you are trying to accomplish something else that would be far simpler to solve than the code sample you provided.
Given your code, plus some "assumptions" of where you might be going, I would recommend something more like so:
$price[1] = 100;
$price[2] = 150;
$price[5] = 225;
$store[1] = "apple";
$store[2] = "orange";
$store[5] = "kiwi";
// Option 1: Global in the variables. Nothing wrong with it here...
function check ( $m ) {
global $price, $store;
$index = array_search( $m, $store );
return ( isset( $price[ $index ] ) ) ? $price[ $index ] : 0;
}
// usage:
echo check( $store[1] );
// Option 2: Pass in all the variables you need
function check ( $m, $price, $store ) {
$index = array_search( $m, $store );
return ( isset( $price[ $index ] ) ) ? $price[ $index ] : 0;
}
// usage:
echo check( $store[1], $price, $store );
// Option 3: Since you seem to know the index already, just pass THAT in
function check ( $index, $price, $store ) {
return ( isset( $price[ $index ] ) ) ? $price[ $index ] : 0;
}
// usage:
echo check( 1, $price, $store );
// Option 4: Globals, plus since you seem to know the index already, just pass THAT in
function check ( $index ) {
global $store, $price;
return ( isset( $price[ $index ] ) ) ? $price[ $index ] : 0;
}
// usage:
echo check( 1 );