1

here i've got my array(the **** are just strings)

        [m_timestamp] => ****
        [n_id] => ****
        [n_name] => ****
        [n_material] => ****
        [n_neck_finish] => ****
        [n_weight] => ****
        [n_height] => ****
        [n_qty_p_ctn] => ****
        [n_ctn_dimensions] => ****
        [n_comment] => ****
        [sha1] => ****

how can i insert another array:

        [n_group] => ****
        [n_available] => ****

into the original one so that it looks like:

        [m_timestamp] => ****
        [n_id] => ****
        [n_name] => ****
        [n_group] => **** //inserted
        [n_available] => **** //inserted
        [n_material] => ****
        [n_neck_finish] => ****
        [n_weight] => ****
        [n_height] => ****
        [n_qty_p_ctn] => ****
        [n_ctn_dimensions] => ****
        [n_comment] => ****
        [sha1] => ****

i know the key value of where to insert the array(in this case: n_name)

What i did:

$pos = intval(array_search("n_name", $myarray))+1;
array_splice($myarray, $pos, 0, $insertedarray);

but it doesn't put the $insertedarray properly, it adds this [0]=>null in the position I specified

how can i solve this?

1

2 Answers 2

6

You can use array_merge function:

$out = array_merge($first_array, $second_array);

UPDATE

Use this to merge your arrays and preserve keys:

// slice $myarray into two parts and insert $insertedarray in between
// keys are preserved
$myarray = array_merge(array_slice($myarray, 0, $pos), $insertedarray, array_slice($myarray, $pos));
Sign up to request clarification or add additional context in comments.

4 Comments

@Tom91136 why is that? you are using associative arrays
i need them to be in order because later on i used next() , previous() alot
Please vote to close question as duplicate as well, you gave the same answer as in the other question.
@FelixKling yeah, it seems so
0

you could use array_push http://php.net/manual/en/function.array-push.php

source: php manual(examples)

<?php

function array_put_to_position(&$array, $object, $position, $name = null)
{
        $count = 0;
        $return = array();
        foreach ($array as $k => $v)
        {  
                // insert new object
                if ($count == $position)
                {  
                        if (!$name) $name = $count;
                        $return[$name] = $object;
                        $inserted = true;
                }  
                // insert old object
                $return[$k] = $v;
                $count++;
        }  
        if (!$name) $name = $count;
        if (!$inserted) $return[$name];
        $array = $return;
        return $array;
}
?>

Example :

<?php
$a = array(
 'a' => 'A',
 'b' => 'B',
 'c' => 'C',
);

print_r($a);
array_put_to_position($a, 'G', 2, 'g');
print_r($a);

/*
Array
(
    [a] => A
    [b] => B
    [c] => C
)
Array
(
    [a] => A
    [b] => B
    [g] => G
    [c] => C
)
*/
?>

2 Comments

i know that but how do i specify the position?
take a look on the comment what you got earlier maybe that one is the workaround what you need

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.