开发者

Simply rearrange/rename array (php)

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 开发者_如何学Cbe 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 !!


This should do:

$rearranged = array();

foreach ($this->data as $data) {
    $rearranged[date('m', $data['date'])][] = array(
        'id'    => array($data['id']),
        'name'  => array($data['name']),
        'day'   => array(date('F', $data['date'])),
        'year'  => array(date('Y', $data['date'])),
        'price' => array($data['price'])
    );
}


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.

As I wrote in comment, after reading this, I would not store all those id, name etc as arrays. $this->month[$month][$n]['id'] is the way to go, not $this->month[$month]['id'][$n], i.e., you need multiple "arrays of id, name etc" rather than "separate array of id, another array of name etc".

function convert(array $data) {
    $result = array();
    foreach ( $data as $value ) {
        $month = date('m', $value['date']);
        $value['day' ] = date('d', $value['date']);
        $value['year'] = date('Y', $value['date']);
        unset($value['date']);
        $result[$month][] = $value;
    }
    return $result;
}

$data = array(
    15 => array(
        'id'    => '15',
        'name'  => 'Name of course',
        'date'  => '1300863780',
        'price' => '0',
    ),
    16 => array(
        'id'    => '16',
        'name'  => 'Name of course',
        'date'  => '1300863780',
        'price' => '0',
    ),
    14 => array(
        'id'    => '14',
        'name'  => 'Name of course',
        'date'  => '1303545780',
        'price' => '0',
    ),
);

print_r(convert($data));

Output:

Array
(
    [03] => Array
        (
            [0] => Array
                (
                    [id] => 15
                    [name] => Name of course
                    [price] => 0
                    [day] => 23
                    [year] => 2011
                )
            [1] => Array
                (
                    [id] => 16
                    [name] => Name of course
                    [price] => 0
                    [day] => 23
                    [year] => 2011
                )
        )
    [04] => Array
        (
            [0] => Array
                (
                    [id] => 14
                    [name] => Name of course
                    [price] => 0
                    [day] => 23
                    [year] => 2011
                )
        )
)


Iterate over the array and change the items accordingly. On the way, collect the new key names and then combine the new array afterwards (Demo):

$keys = array();
foreach($array as &$v)
{
    $keys[] = date('m', $v['date']);
    $v['day'] = date('d', $v['date']);
    $v['year'] = date('Y', $v['date']);
    unset($v['date']);
    foreach($v as &$v2)
        $v2 = array($v2);
    unset($v2);
}
unset($v);
$array = array_combine($keys, $array);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜