gpt4 book ai didi

php - 通过将现有数组中的键剪切到新数组来创建新数组

转载 作者:搜寻专家 更新时间:2023-10-31 21:15:10 24 4
gpt4 key购买 nike

我想获取一个数组并从中剪切一些键(不按顺序)并从中创建一个新数组。

我已使用 array_shift() 函数完成此操作,但到了需要跳过下一个键然后再次执行 array_shift 的地步。

我如何从逻辑上解决这个问题?

我的数组

Array
(
[api] => Array
(
[0] => system
[1] => assets
[2] => theme
[3] => resources
[4] => api
[5] => xml
[6] => json
[7] => jsonp
[8] => request
)

[class] => Array
(
[name] => authentication
[abbr] => auth
)

[directories] => Array
(
[application] => application
[mvc] => Array
(
[model] => model
[view] => view
[controller] => controller
)

[assets] => Array
(
[folder] => assets
[css] => css
[img] => img
[js] => js
)

[config] => config
)

[smarty] => Array
(
[security] => on
[delimiter] => Array
(
[left] => {!
[right] => !}
)

[template] => Array
(
[header] => header
[footer] => footer
[extension] => tpl
)

)

[version] => Array
(
[component] => Array
(
[0] => Array
(
[name] => CMS
[version] => 1.0
)

[1] => Array
(
[name] => TinyMCE jQuery Package
[version] => 3.5
)

[2] => Array
(
[name] => jQuery
[version] => 1.7.2
)

)

)
)

我需要用它们的键创建一个新数组:apiclassversion

最佳答案

创建一个明确的键列表,您希望将这些键从一个数组移动到另一个数组。循环查看该列表,从一个列表中拉出并添加到另一个列表中。然后从原始数组中删除旧副本:

// Original Array, and empty Array
$array = array( 'api' => 1, 'class' => 2, 'fizz' => 3, 'buzz' => 4 );
$newAr = array();

// For each key we'd like to keep
foreach ( array( 'api', 'class' ) as $key ) {
// (Re)move the value from the original array to our new array
$newAr[$key] = $array[$key]; unset( $array[$key] );
}

// Show the contents of the new array
print_r( $newAr );

立即在线试用:http://codepad.org/7iYG4iVB

关于php - 通过将现有数组中的键剪切到新数组来创建新数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10492375/

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