phpQuery : how can I filter some nodes?

105 Views Asked by At

I got a little problem here. I'm parsing JSON data from Reddit (eg. https://www.reddit.com/r/Chilledout.json); and I need to setup a function that would filter the data to remove every post that does not have a media attribute.

When I found out which nodes I want to remove, how can I 'unset' them and return a filtered phpQuery node ?

The code below returns

Fatal error: Cannot use object of type QueryPath\DOMQuery as array in...

PS : Something that can't be changed in my code is that this function receives a phpQuery node; and should return one too.

function get_track_nodes($body_node){

    $selector = '>data >children';
    $options = array(
        'omit_xml_declaration'      => true,
        'ignore_parser_warnings'    => true,
        'convert_from_encoding'     => 'auto',
        'convert_to_encoding'       => 'UTF-8'
    );

    $post_nodes = qp( $body_node, null, $options )->find($selector);

    foreach($post_nodes as $key=>$node) {
        $media = qp( $node, null, self::$querypath_options )->find('media')->innerHTML();
        if (!$media) unset($post_nodes[$key]);
    }

    return $post_nodes;
}
0

There are 0 best solutions below