2016-12-10 67 views
1

我使用下面的代碼刪除陣列,但我覺得有一個問題如何更改陣列特別關鍵數字在PHP

How do I delete a specific array in php

array(2) { 
    [7]=>array(3) { 
    ["title"]=>string(3) "ads" 
    ["number"]=>int(3) 
    [1]=>array(2) { 
     ["address-image"]=>string(63) "http://localhost/wordpress/wp-content/uploads/2017/08/120-3.jpg" 
     ["expire"]=>string(10) "2017/03/10" 
    } 
    [2]=>array(2) { 
     ["address-image"]=>string(63) "http://localhost/wordpress/wp-content/uploads/2016/09/240.jpg" 
     ["expire"]=>string(10) "2016/10/20" 
    } 
    [3]=>array(2) { 
     ["address-image"]=>string(63) "http://localhost/wordpress/wp-content/uploads/2016/10/300.jpg" 
     ["expire"]=>string(10) "2016/12/28" 
    } 
    } 
    ["_multiwidget"]=>int(1) 
} 

運行後:

array(2) { 
    [7]=>array(2) { 
    ["title"]=>string(3) "ads" 
    ["number"]=>int(2) 
    [1]=>array(2) { 
     ["address-image"]=>string(63) "http://localhost/wordpress/wp-content/uploads/2017/08/120-3.jpg" 
     ["expire"]=>string(10) "2017/03/10" 
    } 
    [3]=>array(2) { 
     ["address-image"]=>string(63) "http://localhost/wordpress/wp-content/uploads/2016/10/300.jpg" 
     ["expire"]=>string(10) "2016/12/28" 
    } 
    } 
    ["_multiwidget"]=>int(1) 
} 

但我想稍後改變刪除如下(關鍵從[3]改爲[2]):

array(2) { 
    [7]=>array(2) { 
    ["title"]=>string(3) "ads" 
    ["number"]=>int(2) 
    [1]=>array(2) { 
     ["address-image"]=>string(63) "http://localhost/wordpress/wp-content/uploads/2017/08/120-3.jpg" 
     ["expire"]=>string(10) "2017/03/10" 
    } 
    [2]=>array(2) { 
     ["address-image"]=>string(63) "http://localhost/wordpress/wp-content/uploads/2016/10/300.jpg" 
     ["expire"]=>string(10) "2016/12/28" 
    } 
    } 
    ["_multiwidget"]=>int(1) 
} 

對不起,我的英語不是很好

謝謝

+1

爲什麼關鍵幾乎不相關。使用'foreach'和誰在乎 – RiggsFolly

+1

如果你想將索引從'[3]'更改爲'[2]',使用'array_values'函數。它重新索引並返回數組。 – Perumal

回答

0

後使用array_values創建一個新的數組: 例如:

$arr = array(1, 2, 3); 

unset($arr[0]); 
$arr = array_values($arr); 

print_r($arr); 
//Array ([0] => 2 [1] => 3) 
0

刪除使用array_merge()功能

unset($data[7]["number"][2]); 
$data[7]["number"] = array_merge($data[7]["number"]); 
0

試試這個。假設$a的存儲陣列,並且要交換陣列鍵2和3

$a[3] ^= $a[2] ^= $a[3] ^= $a[2]; 

你想刪除掉,然後取消設置元素之後。