tags:

views:

32

answers:

3

Summary: Given an array

{a, b, ..., w, x, ..., z}

insert several elements {m, n, ..., r} in the position of x, also removing x. Final array:

{a, b, ..., w, m, n, ..., r, ..., z}

Have an return array

$return_arr = array(
                    'saa'=>'A2223',
                    'asab'=>'asB',
                    'wqed'=>'D234',
                    'wqasee'=>'Esd',
                    'wqewf'=>'Ffds',
                    'monwa'=>'monwaaas'//*
                    );

it will return new array if in this array exist this element 'monwa'=>'monwaaas'.And the new array will be the next order of element that we found for example
if we have $return_arr =>the new_array should be

(add two more element ('hi'=>'HI','hello'=>'HELLO')

$new_array = array(
                    'saa'=>'A2223',
                    'asab'=>'asB',
                    'wqed'=>'D234',
                    'wqasee'=>'Esd',
                    'wqewf'=>'Ffds',
                    'hi'=>'HI',
                    'hello'=>'HELLO'
                    );

And if

$return_arr = array(
                    'saa'=>'A2223',
                    'asab'=>'asB',
                    'monwa'=>'monwaaas',//*
                    'wqed'=>'D234',
                    'wqasee'=>'Esd',
                    'wqewf'=>'Ffds'
                    );

the new_array should be:

$new_array = array(
                    'saa'=>'A2223',
                    'asab'=>'asB',
                    'hi'=>'HI',
                    'hello'=>'HELLO',
                    'wqed'=>'D234',
                    'wqasee'=>'Esd',
                    'wqewf'=>'Ffds'
                    );

And so On...

Anybody knows how to do this?

thanks

+1  A: 

Example on an online interpreter.

1) Search the position of that element, 2) remove the element and 3) then insert in its previous position.

$pos = array_search_pos($arr, 'monwa');
unset($arr['monwa']);
$result = array_insert_at($arr, $pos, array("key1"=>"value1", "key2"=>"value2"));

With these functions:

function array_search_pos($arr, $key) {
    $i = 0;
    foreach ($arr as $k => $v) {
        if ("$k" == "$key")
            return $i;
        $i++;
    }
    return false;
}

function array_insert_at($array, $pos, $values) {
    return array_slice($array, 0, $pos, true) +
        $values +
        array_slice($array, $pos, count($array)-$pos, true);
}
Artefacto
@Artefacto$return_arr = array( 'saa'=>'A2223', 'asab'=>'asB', 'wqed'=>'D234', 'wqasee'=>'Esd', 'monwa'=>'monwaaas',//it has problem here 'wqewf'=>'Ffds' );
python
Problem if we put 'monwa'=>'monwaaas' at beginning and after the end.
python
@python It workds at the beginning and at the end. I'm not sure what you mean with "after the end", though.
Artefacto
A: 

You can do like this.

foreach($new_array as $key=>$value)
{
       if($key == 'monwa' && $value =='monwaaas')
       {
               foreach($element_array as $key1=>$value1)
               {
                       $return_arrat[$key1] = $value1;
               }
       }
       else
       {
              $return_array[$key] = $value
       }
}
Maulik Vora
Doesn't keep the positions.
Artefacto
A: 

@Artefacto

This will keep same positions , I am sure.

$return_array will be filed as the positions in $new_array.

$new_array = array(
                    'saa'=>'A2223',
                    'asab'=>'asB',
                    'monwa'=>'monwaaas',//*
                    'wqed'=>'D234',
                    'wqasee'=>'Esd',
                    'wqewf'=>'Ffds'
                    );

$element_array = array('hi'=>'HI','hello'=>'HELLO');

foreach($new_array as $key=>$value)
{
        if($key == 'monwa' && $value =='monwaaas')
        {
               foreach($element_array as $key1=>$value1)
               {
                     $return_array[$key1] = $value1;
           }
        }
    else
    {
               $return_array[$key] = $value;
    }
}

echo "<pre>";
print_r($return_array);
echo "<pre>";

Please try once

Maulik Vora
this should be a comment. And I mean positions, not array keys. E.g. `array('a'=>1, 'b'=>2)` versus `array('b'=>2, 'a'=>1)`
Artefacto
yes,you are right Artefacto
python
sorry for arguing with you talented guys. But I have checked my code.I know it is not efficient for large arrays but I think my answer was correct.try this
Maulik Vora