【问题标题】:Simply rearrange/rename array (php)只需重新排列/重命名数组(php)
【发布时间】:2011-10-03 10:54:14
【问题描述】:

我只是不明白,它实际上是如此基本,但我就是无法实现它。我有以下数组:

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 ) 
)

我只是想稍微重新排列一下,所以它看起来像:

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) ) 
)

解释一下:我想将月份(从 [日期] 计算)作为主键,然后或多或少地列出相应字段/一些新字段中的每个字段。以后同月会有更多的条目,所以这样安排是有意义的。

我得到的是以下内容,为了 foo,我不明白为什么它不能那样工作(而且我无法想出其他方法)。 $this->data 就是上面的数组!

<?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;
        }
    }
?>

我得到的只是 list() 语句中的一堆“未定义偏移”通知。 非常感谢您的帮助!!

【问题讨论】:

  • var_dump($this-&gt;data); 显示了什么?
  • 只是我在上面发布的数组(第一个)。目前正在测试以下答案...
  • 我不会将所有这些 idname 等存储为数组。 $this-&gt;month[$month][$n]['id'] 是要走的路,而不是 $this-&gt;month[$month]['id'][$n]
  • @binaryLV 我明白你的意思,但这是什么原因呢?我只能看到它改变了你循环遍历数组的方式,我似乎并没有真正理解你或我的方式的任何优点或缺点..
  • @Dan Surfrider,它“更具语义化”。以这种方式更容易查看数据组。请参阅下面的答案,它有一个print_r() 输出此类数组 - 您可以清楚地看到与什么相关的内容。至于遍历数组,这样的分组允许你用更干净的foreach 循环替换讨厌的for 循环。

标签: php arrays list foreach


【解决方案1】:

应该这样做:

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

【讨论】:

  • 是的,就这么简单——应该很明显。不过,这里有同样的问题,知道为什么我的 list() 方法不起作用吗?更喜欢我自己的方法...
  • @DanSurfrider:为了让您的 list() 按您的预期工作,您需要放弃 each()while 循环。 list($id, $name, $date, $price) = $field; 会做你想做的事。 each() 返回数组的当前 key / value 对,while() 将因此循环每个 key / value 对。
  • 我已经试过了,有同样的想法,但很失望。虽然很接近 - 正如 hakre 指出的那样,我只需要 array_values() $field。无论如何感谢您的提示:)
【解决方案2】:

解释一下:我想让月份(从 [日期] 开始计算)为 主键,然后或多或少地列出它们中的每个字段 相应的字段/一些新的。以后会有更多 同一个月的条目,所以这样安排是有意义的。

正如我在评论中所写,在阅读完本文后,我不会将所有这些 idname 等存储为数组。 $this-&gt;month[$month][$n]['id'] 是要走的路,而不是 $this-&gt;month[$month]['id'][$n],也就是说,您需要多个“idname 等数组”而不是“id 的单独数组” , 另一个数组 name”。

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

输出:

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
                )
        )
)

【讨论】:

  • 是的,这绝对更干净,也更容易循环。我不知道为什么有时我的生活如此艰难......
【解决方案3】:

遍历数组并相应地更改项目。途中,收集新的键名,然后组合新的数组(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);

【讨论】:

  • +1 以获得正确答案和不同的方式。不过,我更喜欢 deceze 的方法,看起来更简单。无论如何,你有什么想法,为什么我的 list() 方法不起作用?我仍然很困惑,尽管它现在正在工作......我仍然更喜欢我自己的方式,语义上它看起来最干净(至少对我来说)。
  • list() 需要数字键,启用警告,您就会收到通知。你可以试试list() = array_values();——确实,Deceze 方法更容易阅读。
  • 在 php 手册中阅读并无意识地忽略了它。非常感谢!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-12
  • 2015-04-03
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 2012-09-07
相关资源
最近更新 更多