Accessing values in an array [SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
QbertsBrother
Forum Commoner
Posts: 58
Joined: Thu Oct 11, 2007 10:12 am

Accessing values in an array [SOLVED]

Post by QbertsBrother »

i got this code off of php.net

it does exactly what i want it to do but i am having problems accessing a single value in the array

Code: Select all

<?php
$year = '2009';
 
function getMondays($year) {
  $newyear = $year;
  $week = 0;
  $day = 0;
  $mo = 1;
  $mondays = array();
  $i = 1;
  while ($week != 1) {
    $day++;
    $week = date("w", mktime(0, 0, 0, $mo,$day, $year));
  }
  array_push($mondays,date("r", mktime(0, 0, 0, $mo,$day, $year)));
  while ($newyear == $year) {
    $test =  strtotime(date("r", mktime(0, 0, 0, $mo,$day, $year)) . "+" . $i . " week");
    $i++;
    if ($year == date("Y",$test)) {
      array_push($mondays,date("r", $test));
    }
    $newyear = date("Y",$test);
  }
  return $mondays;
}
 
print_r (getMondays($year));
 
?>
that spits out this

Code: Select all

Array ( [0] => Mon, 05 Jan 2009 00:00:00 -0600 [1] => Mon, 12 Jan 2009 00:00:00 -0600 [2] => Mon, 19 Jan 2009 00:00:00 -0600 [3] => Mon, 26 Jan 2009 00:00:00 -0600 [4] => Mon, 02 Feb 2009 00:00:00 -0600 [5] => Mon, 09 Feb 2009 00:00:00 -0600 [6] => Mon, 16 Feb 2009 00:00:00 -0600 [7] => Mon, 23 Feb 2009 00:00:00 -0600 [8] => Mon, 02 Mar 2009 00:00:00 -0600 [9] => Mon, 09 Mar 2009 01:00:00 -0500 [10] => Mon, 16 Mar 2009 01:00:00 -0500 [11] => Mon, 23 Mar 2009 01:00:00 -0500 [12] => Mon, 30 Mar 2009 01:00:00 -0500 [13] => Mon, 06 Apr 2009 01:00:00 -0500 [14] => Mon, 13 Apr 2009 01:00:00 -0500 [15] => Mon, 20 Apr 2009 01:00:00 -0500 [16] => Mon, 27 Apr 2009 01:00:00 -0500 [17] => Mon, 04 May 2009 01:00:00 -0500 [18] => Mon, 11 May 2009 01:00:00 -0500 [19] => Mon, 18 May 2009 01:00:00 -0500 [20] => Mon, 25 May 2009 01:00:00 -0500 [21] => Mon, 01 Jun 2009 01:00:00 -0500 [22] => Mon, 08 Jun 2009 01:00:00 -0500 [23] => Mon, 15 Jun 2009 01:00:00 -0500 [24] => Mon, 22 Jun 2009 01:00:00 -0500 [25] => Mon, 29 Jun 2009 01:00:00 -0500 [26] => Mon, 06 Jul 2009 01:00:00 -0500 [27] => Mon, 13 Jul 2009 01:00:00 -0500 [28] => Mon, 20 Jul 2009 01:00:00 -0500 [29] => Mon, 27 Jul 2009 01:00:00 -0500 [30] => Mon, 03 Aug 2009 01:00:00 -0500 [31] => Mon, 10 Aug 2009 01:00:00 -0500 [32] => Mon, 17 Aug 2009 01:00:00 -0500 [33] => Mon, 24 Aug 2009 01:00:00 -0500 [34] => Mon, 31 Aug 2009 01:00:00 -0500 [35] => Mon, 07 Sep 2009 01:00:00 -0500 [36] => Mon, 14 Sep 2009 01:00:00 -0500 [37] => Mon, 21 Sep 2009 01:00:00 -0500 [38] => Mon, 28 Sep 2009 01:00:00 -0500 [39] => Mon, 05 Oct 2009 01:00:00 -0500 [40] => Mon, 12 Oct 2009 01:00:00 -0500 [41] => Mon, 19 Oct 2009 01:00:00 -0500 [42] => Mon, 26 Oct 2009 01:00:00 -0500 [43] => Mon, 02 Nov 2009 00:00:00 -0600 [44] => Mon, 09 Nov 2009 00:00:00 -0600 [45] => Mon, 16 Nov 2009 00:00:00 -0600 [46] => Mon, 23 Nov 2009 00:00:00 -0600 [47] => Mon, 30 Nov 2009 00:00:00 -0600 [48] => Mon, 07 Dec 2009 00:00:00 -0600 [49] => Mon, 14 Dec 2009 00:00:00 -0600 [50] => Mon, 21 Dec 2009 00:00:00 -0600 [51] => Mon, 28 Dec 2009 00:00:00 -0600 )
what i want to do is access each value individually. i have tried many different ways but cant seem to get it. does anyone know how i would just output the first value?

i have tried things like

Code: Select all

$mondays[0]


what i want to do is eventually write a query based on the date and pull records for the dates.

thanks
Last edited by QbertsBrother on Thu Jan 22, 2009 4:01 pm, edited 1 time in total.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Accessing values in an array

Post by Eran »

The $mondays variable is declared inside the function scope and is not available outside of it. You need to assign the function return value into a variable:

Code: Select all

...
$mondays = getMondays($year);
echo $mondays[0];
QbertsBrother
Forum Commoner
Posts: 58
Joined: Thu Oct 11, 2007 10:12 am

Re: Accessing values in an array

Post by QbertsBrother »

that worked great

thank you very much
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Accessing values in an array [SOLVED]

Post by VladSun »

:)

Code: Select all

function mondays($year)
{
    $result = array();
    $a_week = 7*24*60*60;
    $date = strtotime('first monday', strtotime($year.'-01-01'));
    
    for($i=0; $i<52; $i++)
    {
        $result[] = date('Y-m-d', $date);
        $date += $a_week;
    } 
    return $result;
}
There are 10 types of people in this world, those who understand binary and those who don't
QbertsBrother
Forum Commoner
Posts: 58
Joined: Thu Oct 11, 2007 10:12 am

Re: Accessing values in an array [SOLVED]

Post by QbertsBrother »

thanks VladSun

what you posted works even better!

that is all i really need is the date just as your function returns it.

thanks
Post Reply