Jump to content

get date from specific keys in associative array


russthebarber

Recommended Posts

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.

 

Link to comment
Share on other sites

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');
    }
}

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.