2014-11-25 75 views
1

創造了新的陣列,這是我的數組:如何刪除陣列和在PHP

array(10) { 
    [0]=> string(29) "Cass Luxury Shapewear Bottoms" 
    [1]=> string(8) "xBhjsN9G" 
    [2]=> string(2) "20" 
    [3]=> string(57) "Cass Luxury Shapewear Bottoms. Multiple Styles Available." 
    [4]=> string(29) "cass-luxury-shapewear-bottoms" 
    [5]=> string(28) "Curabitur non nulla sit amet" 
    [6]=> string(8) "vjIhw8cw" 
    [7]=> string(3) "500" 
    [8]=> string(690) "posuere cubilia Curae; Donec velit neque." 
    [9]=> string(28) "curabitur-non-nulla-sit-amet" 
} 

刪除此陣:

[5]=> string(28) "Curabitur non nulla sit amet" 
[6]=> string(8) "vjIhw8cw" 
[7]=> string(3) "500" 
[8]=> string(690) "posuere cubilia Curae; Donec velit neque." 
[9]=> string(28) "curabitur-non-nulla-sit-amet" 

回答

2

使用unset()取消設置一個這樣的數組:

unset($array[5]); //if the index is 5 

舉例:

<?php 

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

    echo "<pre>"; 
    print_r($array); 

    unset($array[1]); 

    echo "<pre>"; 
    print_r($array); 

?> 

輸出:

Array 
(
    [0] => Array 
     (
      [0] => 1 
     ) 

    [1] => Array 
     (
      [0] => 2 
     ) 

) 


Array 
(
    [0] => Array 
     (
      [0] => 1 
     ) 

) 
0

我們需要循環並通過其索引中刪除。

// $items is your array variable 

for ($i = 0; $i < count($items); $i++) { 

    if ($i >= 5 && $i <= 9) { 
     unset($items[$i]); 
    } 
} 

print_r($items); 

希望它可以幫助