doushi5752 2013-12-01 12:18
浏览 43

在数组php中插入“之间”数字键

I am currently writing a class which is going to handle output from other objects. The class constructs a numeric array at the first dimension with HTML "fields", containing tag, attributes and content. Content will be then a numeric array again for HTML tags nesting. If 'Tag' is NULL, it is considered direct text.

$output = array( //Numeric array
    array( //Associative array 
        'Tag' => 'h3',
        'Attributes' => 'id=SomeTitle',
        'Contents' => array (), //Numeric array again, same as first dimension,
    ),
    array( //Associative array 
        'Tag' => 'p',
        'Attributes' => 'id=someID',
        'Contents' => array (), //Numeric array again, same as first dimension
    ),
);

The question now is: is there an easy way to insert data on an already defined position? Let's say I want a new associative array to be placed in $output[1] and all other indexes equal and higher then to be incremented by one.

I know that I could write a method that iterate over the array in reverse and increase all those indexes. It's just that I'm wondering if I overlooked anything in the PHP handbook, that handles the same thing.

  • 写回答

1条回答 默认 最新

  • dousu8456 2013-12-01 12:21
    关注

    You could use array_splice: <kbd>array_splice documentation</kbd>

    array array_splice ( array &$input , int $offset [, int $length [, mixed $replacement = array() ]] )

    Removes the elements designated by offset and length from the input array, and replaces them with the elements of the replacement array, if supplied.

    $input = array("red", "green", "blue", "yellow");
    array_splice($input, 3, 0, "purple");
    // $input is now array("red", "green",
    //          "blue", "purple", "yellow");
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器