Troubles changing numbered array to associative

new to PHP and just barely have a handle on arrays. can’t find a like example of what I’m trying to do here. VERY OVERWHELMED and in GREAT NEED of a gentle bash in the skull to put me on the right path please!

have things running numerically, but associative would be MUCH better, plus I need to learn.

so this combo works fine:
$weekdays_array = array();
while ($info = mysql_fetch_array($query_result))
{
$weekdays_array = array( $info[‘first_hit’],
$info[‘frequency’],
$info[‘weekday’]);
}

$schedule_array = count($weekdays_array);
for ($row=0; $row < $schedule_array; $row++){
$counter = count($weekdays_array[0]);
for ($column=0; $column < $counter; $column++){
print ($weekdays_array[$row][$column] . “<br> “); }
print (”<br>”); }


and here’s my attempt at the same but associative:
$weekdays_array = array();
while ($info = mysql_fetch_array($query_result))
{
$weekdays_array = array($details => array($info[‘first_hit’], $info[‘frequency’], $info[‘weekday’] ));
}

$schedule_array = $weekdays_array;
foreach($schedule_array as $weekdays_array => $value) {
echo $weekdays_array . " : ";
for($j = 0 ; $j < count($schedule_array[$weekday_array]) ; $j++) {
echo $schedule_array[$weekday_array][$j] . " ";
}
echo "
";
}

some detailed comments or suggestions would be really appreciated.
have to run some errands, then will be able to reply.
thanks again!

What kind of associative array you want to get? Got an example?

opps… that would be helpful huh! here’s what I see when all is well:

2010-03-14 // first_hit
14 // frequency
1 // weekday-numeric

2010-03-08
7
2

2010-03-09
7
3

2010-03-31
28
4

2010-03-11
7
5

2010-03-12
14
6

2010-03-27
28
7

That then would be something like


array(
    array(
        'first_hit' => '2010-03-14',
        'frequency' => 14,
        'weekday-numeric' => 1
    ),
    ...
    array(
        'first_hit' => '2010-03-27',
        'frequency' => 28,
        'weekday-numeric' => 7
    )
);

Although, the weekday really doesn’t need to be stored. That can be calculated from the date by using W in the date function

thanks for the input guys. but it was of no help as I did not explain things, sorry. what’s going on here is a dynamic situation - the values are always changing.

the top part of the routine runs once. it builds an array of records from a DB table. in the example posted there are 7 records in the array - one for each weekday. now the bottom part of the routine runs for each and every day of a calendar, checking the array to see what to do about the day currently being processed.

each record contains a:
weekday: the day of week we’re taking about, e.g. 5 (represents Thursdays); frequency: how often to flag this day, e.g. every 14th day;
and first_hit: the date to begin flagging these days, e.g. 2010-03-11.

like I said, things run fine as a numerically keyed array. but I think going associative would make things easier later on, when I start doing something with the data - like asking “is there a record in the array that applies to the day currently running?”.

could really use some direction here please. I’ve been staring at this code too long and am truly lost.

It sounds like you’re wanting to do one of two things:

Either encapsulate the data within a class, so that you can then have methods in that class that do special things with the data

Or, to apply a where clause to the initial database request, so that you’re only getting from the database the information that you need.

I want the former, thank you. but first things first - how do I change my array to associative. at this point I’m so confused I’m not sure if it’s 2D or 3D!

any ideas 57?

You can make them associative by changing

$weekdays_array[] = array($details => array($info['first_hit'], $info['frequency'], $info['weekday'] ));

to

$schedule_array[] = array(
    'first_hit' => $info['first_hit'],
    'frequency' => $info['frequency'],
    'weekday' => $info['weekday']
);

Then you can use things like:


foreach ($schedule_array as $index => $schedule) {
    echo $schedule['frequency'] . '<br>';
}
}

thanks a lot 57!

will have a go at it later this evening!

errr… Warning: Invalid argument supplied for foreach() in…

where did $schedule come from? that’s the problem yes?

The example I gave is not intended to be “plug and play” code that is designed to be dropped in to place.

Instead, the example code is just that, an example, of the type of techniques to use.

I see. appreciate your time 57.