-3

I am iterating trough array in php and I got result like: It's a an array with DOM elements within a DOM Crawler library.

 {
  "data": [
    {
      "content": null,
      "property": null
    },
    {
      "content": "Build your communication strategy and unleash the effectiveness and efficiency of your storytelling",
      "property": null
    }
   }
...

In my code:

    $crawler = new Crawler(file_get_contents($url));
    $items =$crawler->filter('meta');

    $metaData = [];
    foreach ($items as $item) {
            $itemCrawler = new Crawler($item);
            $metaData[] = [
                'content' => $itemCrawler->eq(0)->attr('content'),
                'property' => $itemCrawler->eq(0)->attr('property')
            ];
    }

What I try to accomplish is to get rid of rows where both fields are NULL like first one (if there is one of the fileds, like second one than skip).

Tried with array_filter() but with no success.

return array_filter($metaData, 'strlen');
dispera05
  • 9
  • 3
  • Why remove them after via filtering - just don’t _add_ them to the array in the first place? `if( ! ( is_null($item['value-1']) && is_null($item['value-2']) ) ) { $data[] = …; }` – CBroe Mar 08 '21 at 09:40
  • `array_filter` won't work since you have an array of arrays, and each of those subarrays has two indices, thus not evaluating to falsey value since it's not considered empty (despite the content being null). But why filter afterwards when you can apply the logic while you're building the array? Add an `if` in your `foreach` that only adds a new element to `$data` if both values aren't `null`. – El_Vanja Mar 08 '21 at 09:41

2 Answers2

0

Actually, array_filter() works for empty elements.

Even if the element values are blank, if the keys are there, the blank values will not be removed.

In the code:

$item Has two keys

So, add explicit conditions to check for blank elements and please modify the code like:

$data = [];
$items =$service->get('allData');
foreach ($items as $item) {
    if (! empty($item['content']) && ! empty($item['property'])) {
    $data[] = [
        'content' => $item['content'],
        'property' => $item['property]
    ];
}
}
Pupil
  • 23,834
  • 6
  • 44
  • 66
  • Thank you very much for the answer but I should be more specific. These are DOM elements I am trying to access.. There is the problem. I updated my post. Sorry for that. @Pupil – dispera05 Mar 08 '21 at 10:26
0

Not sure why the first answer wouldn't be accepted. It would be just a bit of tweaks to make it work.

In your loop

$itemCrawler = new Crawler($item);
$content = $itemCrawler->eq(0)->attr('content');
$property = $itemCrawler->eq(0)->attr('property');
if(!is_null($content) || !is_null($property)) {
    $metaData[] = compact('content', 'property');
}

Or if you insist on using array_filter after getting the $metaData array

$filteredMetaData = array_filter($metaData, function($item) {
    return !is_null($item['content']) || !is_null($item['property']);
});
Anurat Chapanond
  • 2,837
  • 3
  • 18
  • 31