标签云

微信群

扫码加入我们

WeChat QR Code

Is there an easy way to delete an element from an array using PHP, such that foreach ($array) no longer includes that element?I thought that setting it to null would do it, but apparently it does not work.


I would not that Konrad answer is the simplest one to the stated problem. With unset() the iterations over the array will not include the removed value anymore. OTOH, it is true that Stevan answer is ample and, actually, was the answer I was looking for - but not the OP :)

2019年05月24日54分00秒

danip Being easy to find in the manual does not preclude a question on StackOverflow.If the question were a duplicate StackOverflow question, then it might not belong here.StackOverflow is a good place to find answers as a go-to option even before looking in the manual.

2019年05月23日54分00秒

unset($array[$key]); $array = array_values($array);

2019年05月24日54分00秒

Related question about removing this in a foreach loop: stackoverflow.com/questions/1949259/…

2019年05月24日54分00秒

If you want to remove keys from array of array (Associative array), see solution at stackoverflow.com/a/47978980/1045444

2019年05月24日54分00秒

if you have <?php $x = array(1, 2, 3, 4); unset($x[2]); var_dump($x); // array(3) { [0]=>int(0), [1]=>int(2), [3]=>int(4), } ?>

2019年05月24日54分00秒

AlexandruRada No, you said “don’t use this” – and that’s just nonsense. You can safely use this method when you treat an array as what it is – a dictionary. Only if you are expecting consecutive numeric indices do you need to use something else.

2019年05月24日54分00秒

Alexander Use array_splice, as described in the other answers.

2019年05月24日54分00秒

unset can have multiple arguments: void unset ( mixed $var [, mixed $... ] ).

2019年05月24日54分00秒

array_filter is also a viable method. Especially good if you don't want to mutate the array but it also doesn't reindex which can be an issue with json_encode. php.net/manual/en/function.json-encode.php#94157

2019年05月24日54分00秒

It's worth noting that when you're using array_splice() you need to know the OFFSET, not the key, but the offset (!) of whatever element you wish to remove

2019年05月23日54分00秒

Tom: For a regular array (that's continuously integer-indexed) the offset is the index. That's where array_splice can make sense (amongst others).

2019年05月23日54分00秒

Yes of course, but just something to remember if you tamper with the array before using splice

2019年05月23日54分00秒

From just a basic test of deleting a ton of elements from a gigantic array, array_splice seems to be a lot quicker and less memory intensive.This matches with what I'd expect: array_values() seems to be making a copy of the array, while array_splice works in place.

2019年05月23日54分00秒

array_values is a useful approach when you are removing elements in a loop and want the indexes to be consistent, but then want to compress them out after the loop.

2019年05月24日54分00秒

$get_merged_values = array_merge($data['res'],$data['check_res']); when i print this print_r($get_merged_values); it displays the following.Array ( [0] => Array ( [menu_code] => 2 [menu_name] => Plant [menu_order_no] => 1 )[1] => Array ( [menu_code] => 3 [menu_name] => Line [menu_order_no] => 2 )) But i need to get the values of menu_code and menu_name using $get_merged_values['menu_code'] and $get_merged_values['menu_name'] respectively, instead of using $get_merged_values[0][menu_code],$get_merged_values[0][menu_name].please help me how to do this?

2019年05月24日54分00秒

Would be good to clarify that this answer is for deleting an element, when you know the value, but not the key. Note that it only deletes the FIRST instance of the value; to find all keys for a value, use array_keys

2019年05月23日54分00秒

$a = array("A"=>1, "B"=>2, "C"=>"a"); print_r($a); unset($a["B"]); print_r($a);gives (formatted):Array ( [A] => 1 [B] => 2 [C] => a ), Array ( [A] => 1 [C] => a )

2019年05月23日54分00秒

It seems you cannot unset array elements indexed by a string (generates "Fatal error: Cannot unset string offsets"). I dont think this was always the case, but certainly as of PHP 5.3.10 and probably earlier

2019年05月24日54分00秒

carpii PHP can unset elements from an associative array. The fatal error is caused when you try to use unset($var['key']) on a string instead of an arrayFor example:$array = array( 'test' => 'value', 'another' => 'value', ); unset($array['test']); // Removes the "test" element from the array as expected $array = 'test'; unset($array['test']); // Throws "Fatal error: Cannot unset string offsets" as expected

2019年05月24日54分00秒

Here you must know the key name, it's better: stackoverflow.com/a/52826684/1407491

2019年05月24日54分00秒

The array_shift re index the key items if it's integer,so it's bad, so you can use this: stackoverflow.com/a/52826684/1407491

2019年05月23日54分00秒

Note that array_shift can only delete the first element in the array. similarly use array_pop to delete the last element in the array.

2019年05月24日54分00秒

Answer is only applicable to the first element of an array and does not answer the general question.

2019年05月24日54分00秒

sebweisgerber you are right but i don't think that is wrong ans and need to downvote this ans . Question is delete an element not mention any position.

2019年05月24日54分00秒

Why is this so underrated?

2019年05月23日54分00秒

What is array_filter used for?

2019年05月24日54分00秒

to remove falsyelements

2019年05月24日54分00秒

php5: simply running array_pop($arr) removes the final entry. No ` = ` needed.

1970年01月01日00分05秒