[英]How to Change Array a Special key to numeric 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)
}
對不起,我的英語不是很好
謝謝
刪除后使用array_merge()
函數
unset($data[7]["number"][2]);
$data[7]["number"] = array_merge($data[7]["number"]);
使用array_values
創建一個新數組:例如:
$arr = array(1, 2, 3);
unset($arr[0]);
$arr = array_values($arr);
print_r($arr);
//Array ( [0] => 2 [1] => 3 )
嘗試這個。 假設$a
正在存儲您的數組,並希望交換數組鍵2和3。
$a[3] ^= $a[2] ^= $a[3] ^= $a[2];
在交換之后,取消設置要刪除的元素。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.