class obj {protected $fields = array('field1','field2');protected $field1 = array();protected $field2 = array();protected loadfields(){}// This will load the $field1 and $field2 with rows of data for the column they describeprotected function clearFields($num){foreach($fields as $field) {unset($this->$field[$num]);// This did not work the line below workedunset($this->{$field}[$num]); // You have to resolve $field first using {}}}}
$stack = array("orange", "banana", "apple", "raspberry");$last_fruit = array_pop($stack);print_r($stack);print_r('Last Fruit:'.$last_fruit); // Last element of the array
<?php// If you want to remove a particular array element use this method$my_array = array("key1"=>"value 1", "key2"=>"value 2", "key3"=>"value 3");
print_r($my_array);if (array_key_exists("key1", $my_array)) {unset($my_array['key1']);print_r($my_array);}else {echo "Key does not exist";}?>
<?php//To remove first array element$my_array = array("key1"=>"value 1", "key2"=>"value 2", "key3"=>"value 3");print_r($my_array);$new_array = array_slice($my_array, 1);print_r($new_array);?>
<?phpecho "<br/> ";// To remove first array element to length// starts from first and remove two element$my_array = array("key1"=>"value 1", "key2"=>"value 2", "key3"=>"value 3");print_r($my_array);$new_array = array_slice($my_array, 1, 2);print_r($new_array);?>
产出
Array ( [key1] => value 1 [key2] => value 2 [key3] =>value 3 ) Array ( [key2] => value 2 [key3] => value 3 )Array ( [key1] => value 1 [key2] => value 2 [key3] => value 3 )Array ( [key2] => value 2 [key3] => value 3 )Array ( [key1] => value 1 [key2] => value 2 [key3] => value 3 )Array ( [key2] => value 2 [key3] => value 3 )
// Create a "numeric" array$animals = array('ant', 'bee', 'cat', 'dog', 'elk', 'fox');print $animals[1]; // Prints 'bee'print $animals[2]; // Prints 'cat'count($animals); // Returns 6
// unset()unset($animals[1]); // Removes element $animals[1] = 'bee'print $animals[1]; // Prints '' and throws an E_NOTICE errorprint $animals[2]; // Still prints 'cat'count($animals); // Returns 5, even though $array[5] is 'fox'
// Add a new element$animals[ ] = 'gnu'; // Add a new element (not Unix)print $animals[1]; // Prints '', still emptyprint $animals[6]; // Prints 'gnu', this is where 'gnu' ended upcount($animals); // Returns 6
// Assign ''$animals[2] = ''; // Zero out valueprint $animals[2]; // Prints ''count($animals); // Returns 6, count does not decrease
// 1 is the index of the first object to get// NULL to get everything until the end// true to preserve keys$array = array_slice($array, 1, null, true);
解决方案#2
// Rewinds the array's internal pointer to the first element// and returns the value of the first array element.$value = reset($array);// Returns the index element of the current array position$key = key($array);unset($array[$key]);
$arr = ['a', 'b', 'c'];
// search the value to find index// Notice! this will only find the first occurrence of value$index = array_search('a', $arr);
if($index !== false){unset($arr[$index]); // $arr = ['b', 'c']}