代码之家  ›  专栏  ›  技术社区  ›  tree em

用多个值替换数组的一个值并保持其位置顺序

php
  •  0
  • tree em  · 技术社区  · 14 年前

    现在我有

    $data = array("red", "green", "blue", "yellow");
    
    $found = "green";
    $expand1 = "apple";
    $expand2 = "other";
    if $data = array("red", "green", "blue", "yellow"); //[1]=>"green"
    I want get the new array look like this
    $result = array("red", "apple","other", "blue", "yellow");
    
    if $data = array("green", "blue", "yellow","red");//[0]=>"green" beginning
    I want get the new array look like this
    $result = array("apple","other", "blue", "yellow","red");
    
    
    if $data = array("blue", "yellow","red","green");//[3]=>"green" the end
    I want get the new array look like this
    $result = array("blue", "yellow","red","apple","other");
    

    任何人都知道苏洛提安。

    谢谢

    1 回复  |  直到 14 年前
        1
  •  4
  •   clumsyfingers    14 年前

    查看php数组拼接函数

    http://php.net/manual/en/function.array-splice.php

    所以…

    $result = array_splice($data,1,1,array("apple","other"));
    

    应该有技巧。