socalnate Posted November 4, 2007 Share Posted November 4, 2007 Been trying to figure out an array issue. For some reason when I echo $key it's echoing "Array". I want it to return the key (in this case adventure). Help appreciated... <?php $action_percent = 1; $adventure_percent = 20; $genre_array = array( 'action'=>$action_percent, 'adventure'=>$adventure_percent ); echo $genre_array['action']; echo $genre_array['adventure']; $max_value = max($genre_array); echo $max_value; $key = array_keys($genre_array, max($genre_array)); echo $key; ?> Quote Link to comment Share on other sites More sharing options...
bwochinski Posted November 4, 2007 Share Posted November 4, 2007 "array_keys()" returns an array of all the keys of the array. You need to refer to $key[0] and $key[1] etc... Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted November 4, 2007 Share Posted November 4, 2007 Looks like you might be looking for a FOREACH loop here. ARRAY_KEYS is returning an array. To see it, try this code real quick: <?php $action_percent = 1; $adventure_percent = 20; $genre_array = array( 'action'=>$action_percent, 'adventure'=>$adventure_percent ); echo $genre_array['action']; echo $genre_array['adventure']; $max_value = max($genre_array); echo $max_value; $key = array_keys($genre_array, max($genre_array)); echo "<pre>"; print_r ($key); echo "</pre>"; ?> To use a FOREACH loop to do the same thing, but control the echoing of each element, do this: <?php $action_percent = 1; $adventure_percent = 20; $genre_array = array( 'action'=>$action_percent, 'adventure'=>$adventure_percent ); echo $genre_array['action']; echo $genre_array['adventure']; $max_value = max($genre_array); echo $max_value; $key = array_keys($genre_array, max($genre_array)); foreach ($key as $value) { echo "$value<br />"; } ?> And of course, building from there, you can dig deeper into the array structure to echo out anything you desire. PhREEEk Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.