2017-06-29 252 views
1

我有以下數組。我想動態更新這個數組。將數組轉換爲關聯數組

Array 
(
[0] => Array 
    (
     [id] => 
     [cip] => 172.35.2.45 
     [ctime] => 1498718978 
     [uip] => 172.35.2.40 
     [utime] => 1498712478 
     [interface] => wan 
     [type] => pass 
     [disabled] => no 
    ) 
[1] => Array 
    (
     [id] => 
     [cip] => 172.35.2.24 
     [ctime] => 1498778578 
     [uip] => 172.35.2.41 
     [utime] => 1498532478 
     [interface] => wan 
     [type] => pass 
     [disabled] => no 
    ) 
) 

我想將它轉換成。

Array 
(
[0] => Array 
    (
     [id] => 
     updated => Array 
     (
      [time] => 1498712478 
      [username] => [email protected] 
     ) 
     created => Array 
     (
      [time] => 1498712478 
      [username] => [email protected] 
     ) 
     [interface] => wan 
     [type] => pass 
     [disabled] => no 
    ) 
[1] => Array 
    (
     [id] => 
     [updated] => Array 
     (
      [time] => 1498532478 
      [username] => [email protected] 
     ) 
     created => Array 
     (
      [time] => 1498778578 
      [username] => [email protected] 
     ) 
     [interface] => wan 
     [type] => pass 
     [disabled] => no 
    ) 
) 

在這裏我們有在主陣列中的兩個元素,它可以是非常,我使用foreach循環和其他一些東西嘗試。但我不接近我想要的答案請幫我排序

回答

0

圖我會將我的評論轉換成一個答案,因爲它是漫長而不守規矩,沒有適當的縮進。 $array1是原始數組

$array2['id'] = $array1['id']; 
$array2['updated'] = ['time'  => $array1['utime'], 
         'username' => '[email protected]'.$array1['uip']]; 
$array2['created'] = ['time'  => $array1['ctime'], 
         'username' => '[email protected]'.$array1['cip']]; 
$array2['interface'] = $array1['interface']; 
$array2['type'] = $array1['type']; 
$array2['disabled'] = $array1['disabled']; 

這基本上只是去一行行你的第二個數組並設置數組元素適當根據您的問題。

我敢肯定,有一種方法可以自動執行您正在嘗試執行的操作,因此您不必逐行執行操作,但實際上,任何自動執行此步驟的答案都是過度殺毒,並且肯定需要更多時間比這個過程。

0
使用 array_combineunset功能

簡短的解決方案:

// $arr is your initial array 
$arr['updated'] = array_combine(['time', 'username'], [$arr['utime'], '[email protected]' . $arr['uip']]); 
$arr['created'] = array_combine(['time', 'username'], [$arr['ctime'], '[email protected]' . $arr['cip']]); 
unset($arr['utime'], $arr['ctime'], $arr['uip'], $arr['cip']); 

print_r($arr); 

輸出:

Array 
(
    [id] => 
    [interface] => wan 
    [type] => pass 
    [disabled] => no 
    [updated] => Array 
     (
      [time] => 1498712478 
      [username] => [email protected] 
     ) 

    [created] => Array 
     (
      [time] => 1498718978 
      [username] => [email protected] 
     ) 
) 
0

我認爲最乾淨的辦法是這樣的:

$array2 = [ 
     'id => '', 
     'updated' => [ 
      'time' => $array1['utime'], 
      'username' => $array1['username'] 
     ], 
     'created' => [ 
      'time' => $array1['ctime'], 
      'username' => $array1['username'] 
     ], 
     'interface' => $array1['wan'], 
     'type' => $array1['pass'], 
     'disabled' => $array1['no'] 
    ] 
]; 

你沒有狀態PHP版本,所以我用方括號來啓動數組,如果你還在小於7,你需要交換array()的所有方括號。

0

我認爲你將有多個條目在你的起始數組中,並希望從較大的初始數組中生成結果數組,因此我通過一個循環。

$array1[] = array(
     'id' => '', 
     'cip' => '172.35.2.45', 
     'ctime' => 1498718978, 
     'uip' => '172.35.2.40', 
     'utime' => 1498712478, 
     'interface' => 'wan', 
     'type' => 'pass', 
     'disabled' => 'no' 
    ); 

$array2 = array(); 
$directkeys = array('id', 'interface', 'type', 'disabled'); 
foreach ($array1 as $mk => $subarray){ 
    foreach($subarray as $k => $v){ 
     if(in_array($k, $directkeys)) $array2[$mk][$k] = $v; 
     if($k == 'uip') $array2[$mk]['id']['updated']['username'] = '[email protected]'.$v; 
     if($k == 'utime') $array2[$mk]['id']['updated']['time'] = $v; 
     if($k == 'cip') $array2[$mk]['id']['created']['username'] = '[email protected]'.$v; 
     if($k == 'ctime') $array2[$mk]['id']['created']['time'] = $v;  
    } 
} 

輸出是:

[0] => Array 
    (
     [id] => Array 
      (
       [created] => Array 
        (
         [username] => [email protected] 
         [time] => 1498718978 
        ) 

       [updated] => Array 
        (
         [username] => [email protected] 
         [time] => 1498712478 
        ) 

      ) 

     [interface] => wan 
     [type] => pass 
     [disabled] => no 
    ) 
+0

您可以通過使用if(){} elseif(){}結構而不是我在此處拋出的一系列ifs來獲得性能上的緩衝 – Dano

0

它已經被做這件事。

foreach($oldarray as $key => $value){ 
    $newarray['created']['time'] = '"'.strtotime($value['ctime']).'"'; 
    $newarray['created']['username'] = '[email protected]'.$value['cip']; 
    $newarray['updated']['time'] = '"'.strtotime($value['utime']).'"'; 
    $newarray['updated']['username'] = '[email protected]'.$value['uip']; 
} 
相關問題