gpt4 book ai didi

php - 多维数组上的 array_unshift

转载 作者:行者123 更新时间:2023-12-01 22:20:40 24 4
gpt4 key购买 nike

我从 var_dump 得到了一个多维数组的结果:

array (size=6)
'sambalpur.in.net' =>
array (size=2)
'classkey' => string 'indotnet' (length=8)
'status' => string 'available' (length=9)
'sambalpur.com' =>
array (size=2)
'classkey' => string 'domcno' (length=6)
'status' => string 'regthroughothers' (length=16)
'sambalpur.info' =>
array (size=2)
'classkey' => string 'dominfo' (length=7)
'status' => string 'regthroughothers' (length=16)
'sambalpur.net' =>
array (size=2)
'classkey' => string 'dotnet' (length=6)
'status' => string 'regthroughothers' (length=16)
'sambalpur.biz' =>
array (size=2)
'classkey' => string 'dombiz' (length=6)
'status' => string 'available' (length=9)
'sambalpur.in' =>
array (size=2)
'classkey' => string 'dotin' (length=5)
'status' => string 'regthroughothers' (length=16)

现在假设我想将这个特定的数组移动到数组的开头:

'sambalpur.biz' => 
array (size=2)
'classkey' => string 'dombiz' (length=6)
'status' => string 'available' (length=9)

我试过:

array_unshift($array,array('sambalpur.biz'));

但是我得到的是这样的:

array (size=7)
0 =>
array (size=1)
0 => string 'sambalpur.biz' (length=13)
'sambalpur.in.net' =>
array (size=2)
'classkey' => string 'indotnet' (length=8)
'status' => string 'available' (length=9)
'sambalpur.com' =>
array (size=2)
'classkey' => string 'domcno' (length=6)
'status' => string 'regthroughothers' (length=16)
'sambalpur.info' =>
array (size=2)
'classkey' => string 'dominfo' (length=7)
'status' => string 'regthroughothers' (length=16)
'sambalpur.net' =>
array (size=2)
'classkey' => string 'dotnet' (length=6)
'status' => string 'regthroughothers' (length=16)
'sambalpur.biz' =>
array (size=2)
'classkey' => string 'dombiz' (length=6)
'status' => string 'available' (length=9)
'sambalpur.in' =>
array (size=2)
'classkey' => string 'dotin' (length=5)
'status' => string 'regthroughothers' (length=16)

移动数组的正确方法是什么?

最佳答案

我以为我以前做过,但找不到重复的:

$array = array_splice($array,
array_search('sambalpur.biz', array_keys($array)), 1) + $array;
  • 使用 array_keys() 获取键的数字索引数组
  • 使用 array_search() 在返回的数组中搜索 sambalpur.biz
  • 使用返回的索引通过 array_splice() 剪切该元素
  • 将其添加到现有数组

Don't Panic 相同:

$array = array_merge(array('sambalpur.biz' => $array['sambalpur.biz']), $array);

无需取消设置,因为插入顺序决定了哪个键会覆盖另一个键,因此这个键会覆盖前一个键。

关于php - 多维数组上的 array_unshift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40312414/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com