array-filter

PHP - Delete Item from Hash Table (array) using array_filter

Hi In PHP, I know there is no official way to delete items once they have been put into an array. But there must be a "best-method" solution to my problem. I believe this may lie in the array_filter function. Essentially, I have a shopping cart object that stores items in a hashtable. Imagine you can only ever buy one of any item at a ...

Filtering email addresses in an array -php

Hello, I have a php array containing email addresses as array(email_address1 => name1, email2 => name2) format. I need to check that emails are valid and I can foreach and foreach($arr as $email => $name) { $new = array(); if(filter_var($email, FILTER_VALIDATE_EMAIL)) { $new[$email] = $name; } return $new; } Can I achie...

Error when using array_filter with static functions in class

i have a class like this class im_a_class { static function not_empty() {...} function render() { return array_filter($array,'self::not_empty') }; } this code works in php 5.3.0 but doesn't work in version 5.2.8. i had to put it out and use it like this function not_empty() {...} class im_a_class { function render() { return...

Use NSPredicate to filter by object attribute

I have a mutable array of custom objects. I want to filter that array by attribute of the object, for example myObject.attributeOne. How can I create the NSPredicate to use with [myArrayOfObjects filterUsingPredicate:<the_predicate>] ...

error in array_filter

Dear all, I have an issue here with filter_array. below is my code: $array = array("0","0","1"); function returnzero($d){ if($d=='0'){ return $d; } } $all_zeros = array_filter($array, "returnzero"); echo count($all_zeros); I would like to filter out all values that none other than zero. Above is my code. However, th...

Can a PHP callback accept its parameter(s) by reference?

I've tested the following and it works on both PHP 5.2 and 5.3, however, it's not documented anywhere as far as I can see so I'm evaluating its use. I have a function in a class called isValid, this checks a hash to see if the given value is in the set of allowed values. There are some values that are valid, but deprecated; I'd like my ...

Why does a class that implements ArrayAccess, Iterator, and Countable not work with array_filter()?

I have the following class: <?php /* * Abstract class that, when subclassed, allows an instance to be used as an array. * Interfaces `Countable` and `Iterator` are necessary for functionality such as `foreach` */ abstract class AArray implements ArrayAccess, Iterator, Countable { private $container = array(); public function ...