rjt90 Posted June 8, 2013 Share Posted June 8, 2013 $array = array(0,0,0,0,0,1,2,3) Is it possible to use array_search() to find the key of the first non-zero element? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/278936-array_search-finding-first-non-zero-element/ Share on other sites More sharing options...
Barand Posted June 8, 2013 Share Posted June 8, 2013 (edited) use array_filter $array = array(0,0,0,0,0,1,2,3); $found = array_filter($array); echo '<pre>',print_r($found, true),'</pre>'; /** result Array ( [5] => 1 [6] => 2 [7] => 3 ) */ edit : or list($nonZeroKey,$value) = each(array_filter($array)); Edited June 8, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/278936-array_search-finding-first-non-zero-element/#findComment-1434863 Share on other sites More sharing options...
rjt90 Posted June 8, 2013 Author Share Posted June 8, 2013 Thank you very much! Quote Link to comment https://forums.phpfreaks.com/topic/278936-array_search-finding-first-non-zero-element/#findComment-1434865 Share on other sites More sharing options...
AbraCadaver Posted June 8, 2013 Share Posted June 8, 2013 To answer the original question, yes: echo array_search(true, $array); Quote Link to comment https://forums.phpfreaks.com/topic/278936-array_search-finding-first-non-zero-element/#findComment-1434872 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.