繁体   English   中英

合并数组数组中的数组

[英]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() ,但这不会处理关联数组。 这里最好的选择是什么?

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

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

你应该能够这样做(没有测试过):

// Get first item off from index 0
$tempData = array_shift($data);

// Add new array $newData at index 0
array_unshift($data, $newData);

// Add old data again at index 0, rest of items should get an incremented index, so $newData is at 1 and ['title'] is at 2
array_unshift($data, $tempData);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM