首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将数组的一个值替换为多个值,并保持其位置顺序

问将数组的一个值替换为多个值,并保持其位置顺序
EN

Stack Overflow用户
提问于 2010-08-20 22:28:33
回答 1查看 136关注 0票数 0

现在我有了

代码语言:javascript
复制
$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");

任何人都知道sulotion.please。

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-08-20 22:33:49

查看php数组拼接函数

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

所以..。

代码语言:javascript
复制
$result = array_splice($data,1,1,array("apple","other"));

应该能行得通。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3531821

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档