合并数组数组中的数组(Merge array in array of arrays)

编程入门 行业动态 更新时间:2024-10-22 14:00:06
合并数组数组中的数组(Merge array in array of arrays)

我目前有一个包含数组数组的数组:

array(9) { ["enabled"]=> array(4) { ["title"]=> string(14) "Enable/Disable" ["type"]=> string(8) "checkbox" ["label"]=> string(25) "Enable" ["default"]=> string(3) "yes" } ["title"]=> array(5) { ["title"]=> string(5) "Title" ["type"]=> string(4) "text" ["description"]=> string(60) "This controls the title which the user sees during checkout." ["default"]=> string(18) "Retail Finance" ["desc_tip"]=> bool(true) }

这个数组叫做$test 。 现在正如你在这个数组中看到的那样,在索引0处有一个名为"enabled"的数组,在索引1处有一个名为"title"的数组。我想在索引0和1之间拼接另一个关联数组。我已经包含了这个下面:

'enable_finance_calculator' => array( 'title' => __( 'Enable Calculator', 'woocommerce' ), 'type' => 'checkbox', 'label' => __( 'Enable Finance Calculator', 'woocommerce' ), 'default' => 'yes' ),

通常在执行此操作时我会使用array_splice() ,但这不会处理关联数组。 这里最好的选择是什么?

I currently have an array that holds an array of arrays:

array(9) { ["enabled"]=> array(4) { ["title"]=> string(14) "Enable/Disable" ["type"]=> string(8) "checkbox" ["label"]=> string(25) "Enable" ["default"]=> string(3) "yes" } ["title"]=> array(5) { ["title"]=> string(5) "Title" ["type"]=> string(4) "text" ["description"]=> string(60) "This controls the title which the user sees during checkout." ["default"]=> string(18) "Retail Finance" ["desc_tip"]=> bool(true) }

This array is called $test. Now as you can see in this array there's an array called "enabled" at index 0, and an array called "title" at index 1. I'd like to splice another associative array between index 0 and 1. I've included this below:

'enable_finance_calculator' => array( 'title' => __( 'Enable Calculator', 'woocommerce' ), 'type' => 'checkbox', 'label' => __( 'Enable Finance Calculator', 'woocommerce' ), 'default' => 'yes' ),

Normally when doing this I'd use array_splice(), but this does not handle associative arrays. What is the best option here?

最满意答案

涉及的种类,但你可以切片和合并:

$test = array_merge( array_slice($test, 0, $pos=array_search('enabled', array_keys($test), true)+1, true), $newarray, array_slice($test, $pos, NULL, true) ); 搜索数组键以查找位置并切片到该位置 合并新阵列 合并从位置到阵列末尾的切片

Kind of involved but you can slice and merge:

$test = array_merge( array_slice($test, 0, $pos=array_search('enabled', array_keys($test), true)+1, true), $newarray, array_slice($test, $pos, NULL, true) ); Search on the array keys to find the position and slice up to that Merge with the new array Merge with a slice from the position up to the end of the array

更多推荐

本文发布于:2023-08-02 09:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1372307.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   组中   Merge   array   arrays

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!