2014-09-23 41 views
0

我不知道,只要在php中間插入數組中的元素。我知道它是如何解決在C++或C#中,但在PHP我不知道。 請幫幫我。如何在中間插入數組中的元素

我用

$stack = array("orange", "banana"); 
array_push($stack, "apple", "raspberry"); 
print_r($stack); 

但這加入開始陣列沒有中間的。

+0

我猜你需要使用密鑰這一點。 – 2014-09-23 09:21:21

+0

使用'array_splice()'。 – Barmar 2014-09-23 09:21:30

回答

2

使用array_splice()

array_splice($stack, 1, 0, array("apple", "raspberry")); 

指定的0長度意味着它應該只需要插入該位置上的新元素,沒有消除任何。

如果你只是插入一個元素到數組,你不需要包裝在一個數組:

array_splice($stack, 1, 0, "apple"); 
+0

如果我只有一個數組,我想在這個數組中插入值? – 2014-09-23 09:25:29

+0

查看已更新回答 – Barmar 2014-09-23 09:27:22

0
$stack = array("orange", "banana"); 
$inserted = array("apple", "raspberry"); 
$position = 1; 
array_splice($stack, $position, 0, $inserted); 
+0

這就對了Barmar,謝謝:) – Uxio 2014-09-23 09:39:55

相關問題