russthebarber Posted September 3, 2012 Share Posted September 3, 2012 I have an associative array with dates and prices: $priceArr=array("2012-9-13"=>"34.00", "2012-9-14"=>"35.00", "2012-9-20"=>"36.00", "2012-10-21"=>"37.00", "2012-10-28"=>"38.00", "2012-10-29"=>"38.00"); I'm looking for the best solution. I need to loop through the array and explode just the keys that have a 9 in the middle (i.e. are in September). Then I just need the date value in a new array....which should look like this when finished: $dateArr=array("13", "14", "20"); The exploding and pushing items into the second array I can do, it's just dealing with the key from the associative array that's new and confusing territory for me. Quote Link to comment https://forums.phpfreaks.com/topic/267965-get-date-from-specific-keys-in-associative-array/ Share on other sites More sharing options...
trq Posted September 3, 2012 Share Posted September 3, 2012 Lost me. Have you got any code? Quote Link to comment https://forums.phpfreaks.com/topic/267965-get-date-from-specific-keys-in-associative-array/#findComment-1374960 Share on other sites More sharing options...
russthebarber Posted September 3, 2012 Author Share Posted September 3, 2012 I'll try and explain it another way. If you look at the keys (dates) in $priceArr, you'll see that there are three dates in September....the 13th, 14h and 20th. This information is in $dateArr. The question is if $dateArr didn't exist how would I create it in php from $priceArr? Quote Link to comment https://forums.phpfreaks.com/topic/267965-get-date-from-specific-keys-in-associative-array/#findComment-1374961 Share on other sites More sharing options...
trq Posted September 3, 2012 Share Posted September 3, 2012 A better question is, how is $priceArr created? Quote Link to comment https://forums.phpfreaks.com/topic/267965-get-date-from-specific-keys-in-associative-array/#findComment-1374974 Share on other sites More sharing options...
salathe Posted September 4, 2012 Share Posted September 4, 2012 You can loop over the array's keys by using something like foreach(array_keys($priceArr) as $date), then inside the loop do whatever checking you need and build the date array. For example: $dateArr = array(); foreach (array_keys($priceArr) as $date) { $datetime = new DateTime($date); if ($datetime->format('F') == 'September') { $dateArr[] = $datetime->format('j'); } } Quote Link to comment https://forums.phpfreaks.com/topic/267965-get-date-from-specific-keys-in-associative-array/#findComment-1375077 Share on other sites More sharing options...
russthebarber Posted September 4, 2012 Author Share Posted September 4, 2012 Brilliant. Thanks salathe. Quote Link to comment https://forums.phpfreaks.com/topic/267965-get-date-from-specific-keys-in-associative-array/#findComment-1375098 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.