Extending PHP - Removing data from an array

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
musashi
Forum Commoner
Posts: 39
Joined: Tue Jul 23, 2002 12:51 pm
Location: Santa Cruz - CA

Extending PHP - Removing data from an array

Post by musashi »

Okay, so I know how to build arrays, and read arrays, but I don't know how to remove elements from arrays.

I would imagine there is a nice macro for it, or it could be something as simple as setting the hash-table entry to NULL, however I have been unsuccessful in doing (or finding how to do) either of these.

Any suggestions? If you know where I could find a complete list of the macros in the PHP system (as the PHP manual only infers that there are many, but does not provide a full listing) I would greatly appreciate it!

Thanks
8O
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

The unset() function is used to delete array values.

Code: Select all

<?php
$my_array["key1"] = "Some value";
$my_array["key2"] = "Another value";

unset($my_array["key2"]);

var_dump($my_array);
?>
You'll then notice that the only thing in the array is:

$my_array["key1"] whose value is "Some value"
User avatar
musashi
Forum Commoner
Posts: 39
Joined: Tue Jul 23, 2002 12:51 pm
Location: Santa Cruz - CA

extending php

Post by musashi »

protokol

I appreciate the assistance, but PHP code is not what I was referring too.

The issue is in regards to extending php (when you build C modules that you compile into the PHP core).

See: http://www.php.net/manual/en/phpdevel.php

I think I might have found a bit more information on it, but I'd still like to know if anyone has tried removing array elements in an existing array (one that is passed in as a parameter to a function). Currently the best thing I can do is to do a manual copy of the array contents (excluding what I want to remove) and then returning the new array. This seems combersome though.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

probably ZEND_API int zend_hash_del_key_or_index(HashTable *ht, char *arKey, uint nKeyLength, ulong h, int flag) is what you're looking for.
Take a look at how the modules/core code use it.
It's mostly referenced as zend_hash_del(...) or zend_hash_index_del(...)
Post Reply