Array_Pluck Conditional

3 posts by 2 authors in: Forums > CMS Builder
Last Post: July 26, 2012   (RSS)

By Perchpole - July 25, 2012

Hello, All -

I'm trying to create an IF statement to test whether array_pluck returns any results.

Something like this...

<?php if(count(array_pluck($branchRecords, 'attachments')) > 0): ?>

Unfotunately it doesn't seem to work. Even when the array is empty it returns true.

What am I doing wrong?

:o/

Perch

Re: [Perchpole] Array_Pluck Conditional

By Jason - July 25, 2012

Hi Perch,

Because the field will always exist in each record, even if empty, array_pluck will always return 1 array element for each record passed into it.

You can get around this by using array_filter() to get rid of any empty values like this:

<?php if(count(array_filter(array_pluck($branchRecords, 'attachments'))) > 0): ?>

Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/