我有以下数组。我想动态更新这个数组。
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] => admin@172.35.2.40
)
created => Array
(
[time] => 1498712478
[username] => admin@172.35.2.45
)
[interface] => wan
[type] => pass
[disabled] => no
)
[1] => Array
(
[id] =>
[updated] => Array
(
[time] => 1498532478
[username] => admin@172.35.2.41
)
created => Array
(
[time] => 1498778578
[username] => admin@172.35.2.24
)
[interface] => wan
[type] => pass
[disabled] => no
)
)
这里我们在主数组中有两个元素,它可以非常,我尝试使用foreach循环和其他一些东西。但我不想接近我想要的答案请帮助我排序这个
答案 0 :(得分:0)
想想我会将我的评论转换成答案,因为如果没有适当的缩进,它会很长且不守规矩。 false
是原始数组
$array1
这基本上只是逐行排列第二个数组,并根据你的问题正确设置数组元素。
我确信有一种方法可以自动完成您要尝试的操作,因此您不必逐行进行,但老实说,任何自动执行此步骤的答案都是过度的,并且肯定会花费更多时间来处理比这个。
答案 1 :(得分:0)
使用array_combine
和unset
函数的简短解决方案:
// $arr is your initial array
$arr['updated'] = array_combine(['time', 'username'], [$arr['utime'], 'admin@' . $arr['uip']]);
$arr['created'] = array_combine(['time', 'username'], [$arr['ctime'], 'admin@' . $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] => admin@172.35.2.40
)
[created] => Array
(
[time] => 1498718978
[username] => admin@172.35.2.45
)
)
答案 2 :(得分: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(); < / p>
答案 3 :(得分: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'] = 'admin@'.$v;
if($k == 'utime') $array2[$mk]['id']['updated']['time'] = $v;
if($k == 'cip') $array2[$mk]['id']['created']['username'] = 'admin@'.$v;
if($k == 'ctime') $array2[$mk]['id']['created']['time'] = $v;
}
}
输出是:
[0] => Array
(
[id] => Array
(
[created] => Array
(
[username] => admin@172.35.2.45
[time] => 1498718978
)
[updated] => Array
(
[username] => admin@172.35.2.40
[time] => 1498712478
)
)
[interface] => wan
[type] => pass
[disabled] => no
)
答案 4 :(得分:0)
这是通过这样做完成的。
foreach($oldarray as $key => $value){
$newarray['created']['time'] = '"'.strtotime($value['ctime']).'"';
$newarray['created']['username'] = 'admin@'.$value['cip'];
$newarray['updated']['time'] = '"'.strtotime($value['utime']).'"';
$newarray['updated']['username'] = 'admin@'.$value['uip'];
}