unset array value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

unset array value

Post by gurjit »

hi,

how can i search through an array and unset a value

my array is for example:

Code: Select all

$enquiry_array = array ( 
1 => '222', 
2 => '223' 
3 => '224' 
);
i tried this code and it did not work

Code: Select all

<?php
if (in_array($eqid, $enquiry_array))
{  

   unset($enquiry_array[array_search($eqid, $enquiry_array)]); // $key = 0;
}
?>
I want to unset the value which will be passed through as $eqid
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

It worked for me using the following code:

Code: Select all

<?php

$enquiry_array = array (
	1 => '222',
	2 => '223',
	3 => '224'
);

$eqid = 223;

if (in_array($eqid, $enquiry_array)) { 
   unset($enquiry_array[array_search($eqid, $enquiry_array)]); // $key = 0;
}

echo '<pre>';
print_r($enquiry_array);
echo '</pre>';

?>
Are you sure that $eqid is being passed correctly?

Mac
Post Reply