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. 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 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)); 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! 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); 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
Archived
This topic is now archived and is closed to further replies.