How to Remove a Key and Its Value from an Associative Array

How can I remove a key and its value from an associative array?

You can use unset:

unset($array['key-here']);

Example:

$array = array("key1" => "value1", "key2" => "value2");
print_r($array);

unset($array['key1']);
print_r($array);

unset($array['key2']);
print_r($array);

Output:

Array
(
[key1] => value1
[key2] => value2
)
Array
(
[key2] => value2
)
Array
(
)

PHP Remove elements from associative array

Your array is quite strange : why not just use the key as index, and the value as... the value ?

Wouldn't it be a lot easier if your array was declared like this :

$array = array(
1 => 'Awaiting for Confirmation',
2 => 'Asssigned',
3 => 'In Progress',
4 => 'Completed',
5 => 'Mark As Spam',
);

That would allow you to use your values of key as indexes to access the array...

And you'd be able to use functions to search on the values, such as array_search() :

$indexCompleted = array_search('Completed', $array);
unset($array[$indexCompleted]);

$indexSpam = array_search('Mark As Spam', $array);
unset($array[$indexSpam]);

var_dump($array);

Easier than with your array, no ?



Instead, with your array that looks like this :

$array = array(
array('key' => 1, 'value' => 'Awaiting for Confirmation'),
array('key' => 2, 'value' => 'Asssigned'),
array('key' => 3, 'value' => 'In Progress'),
array('key' => 4, 'value' => 'Completed'),
array('key' => 5, 'value' => 'Mark As Spam'),
);

You'll have to loop over all items, to analyse the value, and unset the right items :

foreach ($array as $index => $data) {
if ($data['value'] == 'Completed' || $data['value'] == 'Mark As Spam') {
unset($array[$index]);
}
}
var_dump($array);

Even if do-able, it's not that simple... and I insist : can you not change the format of your array, to work with a simpler key/value system ?

Remove only Key from Multidimensional Associative Array in PHP

You can do this with simply using two foreach() loop and push the value to attr_id

$expected = [];        
foreach($collections as $k=>$v){
foreach($v as $k1=>$v1){
$expected[$v1['attr_id']][] = $v1['value'];
}
}
print_r($expected);

Output:

Array (
[23] => Array (
[0] => Single Side
[1] => Double Side
)
[31] => Array (
[0] => A4
[1] => 12x18
[2] => B5
[3] => A5
)
)

DEMO: https://3v4l.org/JlsIl

Remove an associative array by key from finding a value in that array in PHP

Filter your array with a callback like this:

$docsToRemove = explode(',',$this->postData['documents']); //create array of names we want to remove from $docs

$filtered = array_filter(
$docs,
function ($doc) use ($docsToRemove) { return !in_array($doc->getName(), $docsToRemove); }
);

Delete a key from an associative array

Try delete person["name"].

Notice that delete will only set it as undefined, which will then not be reflected correctly in the length of the array.

If you know the key you should use splice i.e.

myArray.splice(key, 1);

PHP - Remove and return an element from an associative array

Looking quickly at the official PHP documentation, in the current version (7.2) doesn't have a function that removes and returns an element by the key.

But as you mentioned there are several ways to solve this problem. As you can see at: https://stackoverflow.com/a/10898827/4214312

How to remove an element from a key value array?

You can use the .filter method on the array combined with the Object.keys to clean the function up a lot:

removeSplitAmount(e) {
const newSplitAmount = this.state.splitAmount
.filter(p => !Object.keys(p).includes(e.target.name));

this.setState({ splitAmount: newSplitAmount });
}


Related Topics



Leave a reply



Submit