2012-03-20 91 views
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") 
); 

這個

$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?

我已經嘗試過從不同的帖子應用不同的方法,但沒有成功。 我需要的數組被嵌套這樣我就可以重新排序的「類型」的數組.Hi

+0

你嘗試使用[array_walk()](http://php.net/manual/en/function.array-walk.php)?或只是循環遍歷數組可能會更容易 – ianbarker 2012-03-20 17:25:17

回答

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

但是,如果有更多的信息添加到事件數組中,此代碼將不得不被修改以保持它... – keithhatfield 2012-03-20 17:32:35

+0

我同意@dleiftah他需要修改...有更新了替代方案的答案 – Baba 2012-03-20 17:37:22

0

只是遍歷和重組......

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

print_r($result); 
0

這裏就是我所做的:

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

print_r($array_processed); 
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; 
    } 
} 

以上應該工作。

0

最短的代碼來做到這一點應該是:

foreach ($events as $event) 
    $events_processed[$event['type']][] = array('title' => $event['title'], 'nid' => $event['nid']); 
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 
       ) 

     ) 

)