我有一个关联数组:
style = @doc.at_css "style"
body = @doc.at_css "body"
style.parent = body
first_child = body.first_element_child
first_child.add_previous_sibling(style)
现在我希望将顶部的包装ID位置更改为:
$headers=
Array
(
[MODULE_ID] => Module ID
[PLATFORM_ID] => Platform ID
[PACKAGE_GUID] => Package GUID
[AGGREGATE_PACKAGE] => Aggregate Package
[DELD] => Deld
)
我尝试了 $desired=
Array
(
[PACKAGE_GUID] => Package GUID
[MODULE_ID] => Module ID
[PLATFORM_ID] => Platform ID
[AGGREGATE_PACKAGE] => Aggregate Package
[DELD] => Deld
)
方法,但在这种情况下它不起作用,因为它是一个关联数组。
array_unshift
我怎样才能实现它? 感谢。
答案 0 :(得分:0)
这是一种方法:
$arr = array_merge(array($key => $value), $arr);
$arr
是您的数组,$key
和$value
是要移到顶部的数组的相应值。
e.g。
$arr = array_merge(array('PACKAGE_GUID' => $arr['PACKAGE_GUID']), $arr);
答案 1 :(得分:0)
unset($headers['PACKAGE_GUID']);
$desired = array( 'PACKAGE_GUID' => Package GUID ) + $headers;
如果您需要已设置的值,可以使用:
$value = $headers['PACKAGE_GUID'];
unset($headers['PACKAGE_GUID']);
$desired = array( 'PACKAGE_GUID' => $value ) + $headers;
答案 2 :(得分:0)
unset($headers['PACKAGE_GUID']);
array_unshift($headers, array('PACKAGE_GUID' => 'Package GUID'));
答案 3 :(得分:0)
你可以通过简单地分配你想要的数组来实现这个目标
$headers= Array
(['MODULE_ID'] => 'Module ID',
['PLATFORM_ID'] => 'Platform ID',
['PACKAGE_GUID'] => 'Package GUID',
['AGGREGATE_PACKAGE'] => 'Aggregate Package',
['DELD'] => 'Deld');
根据需要输出:
$desired=
Array
(
[PACKAGE_GUID] => $headers['PACKAGE_GUID'],
[MODULE_ID] => $headers['MODULE_ID'],
[PLATFORM_ID] => $headers['PLATFORM_ID'],
[AGGREGATE_PACKAGE] =>$headers['AGGREGATE_PACKAGE'],
[DELD] => $headers['Deld']
);