0

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.

0

1 Answer 1

2

You could use array_splice: array_splice documentation

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");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.