Mahngiel Posted March 21, 2012 Share Posted March 21, 2012 Looking for the best method to conditionally strip leading zeros for the following situation: $a = array(01, 02, 03, ... 10, 11); $a < 10 ? $a = ? : ''; For the following result: 1, 2, 3, ... 10, 11 Is there a better method than explode? Quote Link to comment https://forums.phpfreaks.com/topic/259431-strip-leading-zero-if-exists/ Share on other sites More sharing options...
Psycho Posted March 21, 2012 Share Posted March 21, 2012 Huh? explode() won't do anything to an array - it is used to create arrays. Why don't you show exactly how you are getting the data and how you are using it and what your desired result is? EDIT: In fact, since you are defining those values a numbers and not strings they will automatically have the leading zeros stripped when they are initially assigned. So, your request makes even less sense. Although I just found that 08 and 09 are converted to 0 for some odd reason $a = array(01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11); echo $a[0]; //Output: 1 echo implode(', ', $a); //Output 1, 2, 3, 4, 5, 6, 7, 0, 0, 10, 11 Quote Link to comment https://forums.phpfreaks.com/topic/259431-strip-leading-zero-if-exists/#findComment-1329900 Share on other sites More sharing options...
AyKay47 Posted March 21, 2012 Share Posted March 21, 2012 $a = array(01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11); $a_w = array_walk($a, function($v,$k) use(&$a) { ltrim($v, 0); }); if($a_w) { print_r($a); } results: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 0 [8] => 0 [9] => 10 [10] => 11 ) This is pseudo code, I'm sure a better way exists, but I'm having a brain fart. Quote Link to comment https://forums.phpfreaks.com/topic/259431-strip-leading-zero-if-exists/#findComment-1329904 Share on other sites More sharing options...
Mahngiel Posted March 21, 2012 Author Share Posted March 21, 2012 I am passing two different day formats from different tables (one 'j', the other 'd') through a calendar class, and the calendar accepts 'j'. The array key of [1] works, but key [01] does not. Some code // this day is in 'j' format, $events = $this->CI->events->get_events(array('event_month' => date('n', $time))); // this dat is in 'd' format $matches = $this->CI->matches->get_matches_like((date('Y-m', $time))); // this runs off 'j' $calendar .= '<td class="' . ($day == date('j', $time) ... Quote Link to comment https://forums.phpfreaks.com/topic/259431-strip-leading-zero-if-exists/#findComment-1329905 Share on other sites More sharing options...
Mahngiel Posted March 21, 2012 Author Share Posted March 21, 2012 ltrim($v, 0); Cheers! $match->day < 10 ? $match->day =ltrim($match->day, 0): ''; Quote Link to comment https://forums.phpfreaks.com/topic/259431-strip-leading-zero-if-exists/#findComment-1329907 Share on other sites More sharing options...
dragon_sa Posted March 21, 2012 Share Posted March 21, 2012 PHP will treat numbers with a leading 0 as octal numbers, either drop the leading 0 or put the key values in quotes, thats why 08 and 09 become 0 and it starts again at 10 Quote Link to comment https://forums.phpfreaks.com/topic/259431-strip-leading-zero-if-exists/#findComment-1329908 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.