使用PHP将数组组织到嵌套数组中

时间:2012-03-20 17:20:59

标签: php arrays multidimensional-array

有没有人知道如何转换

$events = array(
array("type" => "off-site", "title" => "aaa", "nid" => "11"),
array("type" => "off-site", "title" => "bbb", "nid" => "22"),
array("type" => "installation", "title" => "ccc", "nid" => "33"),
array("type" => "opening", "title" => "ddd", "nid" => "44"),
array("type" => "opening", "title" => "eee", "nid" => "55"),
array("type" => "opening", "title" => "fff", "nid" => "66")
);

进入这个

$events_processed = array(
"off-site" => array(
        array(
            "title" => "aaa",
            "nid" => "11"
        ),
        array(
            "title" => "bbb",
            "nid" => "22"
        )
),
"installation" => array(
        array(
            "title" => "ccc",
            "nid" => "33"
        )
),
"opening" => array(
        array(
            "title" => "ddd",
            "nid" => "44"
        ),
        array(
            "title" => "eee",
            "nid" => "55"
        ),
        array(
            "title" => "fff",
            "nid" => "66"
        )
)
);

使用php?

我已经尝试在不同的帖子中应用不同的方法,但没有成功。 我需要嵌套数组,所以我可以通过“type”.Hi

重新排序数组

6 个答案:

答案 0 :(得分:1)

您可以使用下面的代码,请注意您的当前输出无效,因为您错过了数组索引......

示例1

    $events_processed = array();
    foreach($events as $options)
    {
        $events_processed[$options['type']][] = array("title"=>$options['title'],"nid"=>$options['nid']);
    }
    var_dump($events_processed);

OR

示例2(@dleiftah建议)

    $defautKey = "type" ; 
    foreach($events as $options)
    {
        $type = $options[$defautKey] ;
        unset($options[$defautKey]);
        $events_processed[$type][] = $options;
    }
     var_dump($events_processed);

两个结果都是这样的,但是数字2更灵活

        array
          'off-site' => 
            array
              0 => 
                array
                  'title' => string 'aaa' (length=3)
                  'nid' => string '11' (length=2)
              1 => 
                array
                  'title' => string 'bbb' (length=3)
                  'nid' => string '22' (length=2)
          'installation' => 
            array
              0 => 
                array
                  'title' => string 'ccc' (length=3)
                  'nid' => string '33' (length=2)
          'opening' => 
            array
              0 => 
                array
                  'title' => string 'ddd' (length=3)
                  'nid' => string '44' (length=2)
              1 => 
                array
                  'title' => string 'eee' (length=3)
                  'nid' => string '55' (length=2)
              2 => 
                array
                  'title' => string 'fff' (length=3)
                  'nid' => string '66' (length=2)

答案 1 :(得分:0)

只需循环并重新组织......

$result = array();
foreach($events as $event){
    $key = $event['type'];
    unset($event['type']);
    $result[$key][] = $event;
}

print_r($result);

答案 2 :(得分:0)

这就是我的所作所为:

$array_processed = array();
foreach( $events as $evt){
    $array_processed[$evt['type']][] = array('title'=>$evt['title'], 'nid'=>$evt['nid']);
}

print_r($array_processed);

答案 3 :(得分:0)

没有内置功能可以执行此操作。您将不得不遍历此数组并创建一个新数组。

$events=array(contents-of-array);
$proc_events=array(
    'off-site' => array(),
    'installation' => array(),
    'opening' => array()
);
foreach($events as $key => $value)
{
    switch($value['type'])
    {
        case 'off-site':
        $proc_events['offsite']=array('title' => $value['title'], 'nid' => $value['nid']);
        break;

        case 'installation':
        $proc_events['installation']=array('title' => $value['title'], 'nid' => $value['nid']);
        break;

        case 'opening':
        $proc_events['opening']=array('title' => $value['title'], 'nid' => $value['nid']);
        break;
    }
}

上述情况应该有效。

答案 4 :(得分:0)

执行此操作的最短代码应为:

foreach ($events as $event)
    $events_processed[$event['type']][] = array('title' => $event['title'], 'nid' => $event['nid']);

答案 5 :(得分:0)

以下是我的能力:

<?php
$events = array(
    array("type" => "off-site", "title" => "aaa", "nid" => "11"),
    array("type" => "off-site", "title" => "bbb", "nid" => "22"),
    array("type" => "installation", "title" => "ccc", "nid" => "33"),
    array("type" => "opening", "title" => "ddd", "nid" => "44"),
    array("type" => "opening", "title" => "eee", "nid" => "55"),
    array("type" => "opening", "title" => "fff", "nid" => "66")
);

foreach ($events as $event) {
    $events_processed[$event['type']][] = array('title' => $event['title'],
                                   'nid' => $event['nid']
    );
}
echo '<pre>';
print_r($events_processed);
?>

这将打印以下内容:

Array
(
    [off-site] => Array
        (
            [0] => Array
                (
                    [title] => aaa
                    [nid] => 11
                )

            [1] => Array
                (
                    [title] => bbb
                    [nid] => 22
                )

        )

    [installation] => Array
        (
            [0] => Array
                (
                    [title] => ccc
                    [nid] => 33
                )

        )

    [opening] => Array
        (
            [0] => Array
                (
                    [title] => ddd
                    [nid] => 44
                )

            [1] => Array
                (
                    [title] => eee
                    [nid] => 55
                )

            [2] => Array
                (
                    [title] => fff
                    [nid] => 66
                )

        )

)