Page 1 of 1

Extending PHP - Removing data from an array

Posted: Thu Mar 27, 2003 1:03 pm
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

Posted: Thu Mar 27, 2003 2:06 pm
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"

extending php

Posted: Thu Mar 27, 2003 2:19 pm
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.

Posted: Thu Mar 27, 2003 3:58 pm
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(...)