Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting contents of multidimensional arrays

Status
Not open for further replies.

rycrostud

Programmer
Jul 9, 2001
4
GB
Here's a good one. It's giving me a real headache.

Normally, if you have an array

e.g. $array = array($val1,$val2,$val3);

you can delete the contents by doing something like:

unset ($array[2]);

would delete '$val2' from the array.

How do you delete the contents of a 2 dimensional array of this kind of construct?:

$array1 = array($array2 = array(id => $id, code => $code, price => $price, qty => $qty));

i.e. I need to delete an entire $array2 from $array1.

unset() doesn't seem to work in this case. I guess it's because I'm trying to delete an array rather than the contents of an array.

An ideas?
 
Hard question.

I found this:
$key_index = array_keys(array_keys($array), $target_key);
array_splice($array, $key_index[0], 1);

I'll suppose you have something like this:

$array = (ID => array (INFO));

You can try to do something like:

unset($array['2']); // you should remove the key '2' from the array.

orelse

$key_index = array_keys(array_keys($array), 2);
array_splice($array, $key_index[0], 1);


Should work as well.

P.S. i got this info in Anikin
Hugo Alexandre Dias
Web-Programmer
anikin@ip.pt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top