简体   繁体   中英

Substitute in place an element with two other in a PHP array

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

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

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

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
       }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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