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');
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']);
});