Jump to content

Trying to coalesce values using WireArray::each()


Recommended Posts

So I'm trying to wrap my head around what each() can do and when I need to use a regular foreach() structure.

I have the following code:

$pages->find('template=social-media-platform, !social_media_share_url=""')->each(function($zxc,$val){
    $smi = ($val->social_media_alt_icon<>'') ? $val->social_media_alt_icon : $val->social_media_icon;
    $ary[$zxc] = array(
        "social_media_share_url" => $val->social_media_share_url,
        "social_media_share_parameters" => $val->social_media_share_parameters,
        "social_media_icon" => $smi
        );
    return $ary[$zxc];
    })

What I am trying to do is generate an array that is a subset of the values normally passed through the system, and trying to coalesce two value so that if the default value is empty and an alternative is not, the alternative is plugged into the same default value key.

Regardless of whether I return $ary or $ary[$zxc] what I get back is a PageArray with page fields I have referred to in the loop included, but the substitution isn't made.

I will do this with a regular foreach() but I guess I am trying to understand what the purposes/limitation of the keyed function argument are if you always get a PageArray as the result. What I'm trying to get is something simpler. Or I can actually use the PageArray object and just do the coalesce logic at output.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...