Cristian Badea
Cristian Badea

Reputation: 61

How to remove items with certain length from array in php

I am trying for a while now to remove values for certain length from an array, an I can't seem to get it done. I have removed white spaces and emtpy arrays using the built in functions, but I also want to remove items from the array that have less than 30 characters.

My code is:

$data = array();
$cells = $dom->getElementsByTagName('td');
foreach($cells as $node) {
    foreach($node->childNodes as $child) {
        $data[] = array($child->nodeName => $child->nodeValue); 
        $data = array_map('array_filter', $data);
        $data = array_filter($data);
    }
}

Upvotes: 1

Views: 140

Answers (1)

atoms
atoms

Reputation: 3093

I would change your function to something like this:

foreach($cells as $node){
    foreach($node->childNodes as $child) {
        // check the length is greater than or equal to 30
        if(strlen($child->nodeValue) >= 30 ){

            $data[ $child->nodeName ] =  $child->nodeValue; 

            $data[ $child->nodeName ] = array_map('array_filter', $aTempData);
            $data[ $child->nodeName ] = array_filter($aTempData);

        }
    }
}

If you wanted to remove from starting array you can use unset() as with your original method

Upvotes: 1

Related Questions