I just don’t get it, it’s actually so basic, but I just can’t make it happen. I’ve got the following array:
Array (
[15] => Array (
[id] => 15
[name] => Name of course
[date] => 1300863780
[price] => 0 )
[14] => Array (
[id] => 14
[name] => Name of course
[date] => 1303545780
[price] => 0 )
)
And I just want to rearrange it a little bit, so it looks like:
Array (
[03] => Array (
[id] => Array(15)
[name] => Array(Name of course)
[day] => Array(23)
[year] => Array(2011)
[price] => Array(0) )
[04] => Array (
[id] => Array(14)
[name] => Array(Name of course)
[day] => Array(23)
[year] => Array(2011)
[price] => Array(0) )
)
To explain it: I want to make the month (calculated from [date]) to be the main key and then more or less list each field in their corresponding fields/some new ones. Later on there will be more entries in the same month, so it makes sense to arrange it that way.
What I got yet is the following, and for the sake of foo, I don’t understand why it doesn’t work that way (and no other way I can come up with). $this->data is the array above!
<?php
foreach($this->data as $field)
{
while(list($id, $name, $date, $price) = each($field))
{
$month = date('m',$date);
$this->month[$month]['id'] = $id;
$this->month[$month]['name'] = $name;
$this->month[$month]['day'] = date('F',$date);
$this->month[$month]['year'] = date('Y',$date);
$this->month[$month]['price'] = $price;
}
}
?>
All I get are heaps of ‘undefined offset’ notices in the line with the list() statement.
Much appreciate your help !!
As I wrote in comment, after reading this, I would not store all those
id,nameetc as arrays.$this->month[$month][$n]['id']is the way to go, not$this->month[$month]['id'][$n], i.e., you need multiple “arrays ofid,nameetc” rather than “separate array ofid, another array ofnameetc“.Output: