合并数组数组中的数组(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更多推荐
发布评论