简体   繁体   English

在PHP数组中替换另外两个元素

[英]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 . x的位置插入几个元素{m, n, ..., r} ,同时删除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 它将返回新数组,如果在此数组中存在此元素'monwa'=>'monwaaas'。新数组将是我们找到的下一个元素顺序,例如
if we have $return_arr =>the new_array should be 如果我们有$ return_arr => new_array应该是

(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应该是:

$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. 1)搜索该元素的位置,2)移除元素,3)然后插入其先前的位置。

$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

This will keep same positions , I am sure. 我相信这将保持相同的立场。

$return_array will be filed as the positions in $new_array. $ return_array将作为$ 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
       }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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