Forum

Thread tagged as: Question

Why is an array variable not working in `perch_collection`

I'm trying to pass an array of values into a Perch collection.

I'm guessing that this is fundamentally a PHP question but I'd be really grateful if someone could point out why

This works:

$filter = array('accents/spanish', 'accents/german', 'accents/welsh', 'accents/japanese', 'accents/german',);

perch_collection('Artists',
    array(
    'category' => $filter,
    )
);

But this does not work:

$accents = "'accents/spanish', 'accents/german', 'accents/welsh', 'accents/japanese', 'accents/german',";
$filter = array($accents);

perch_collection('Artists',
    array(
    'category' => $filter,
    )
);
Jay George

Jay George 2 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

$accents is just a string. It's one array item, not multiple.

ok thanks, I'm still confused why the first instance works though.

Is there a way to parse it correctly? Can I turn it into an array item, somehow?

I just found an article that suggests how to turn it into an array, but this still isn't working:

$accents = "'accents/spanish', 'accents/german', 'accents/welsh', 'accents/japanese', 'accents/german',";
$arr=explode(",",$accents);
$filter = array($arr);

perch_collection('Artists',
    array(
        'category' => $filter,
    )
);
Drew McLellan

Drew McLellan 2638 points
Perch Support

The first instance is an array of strings.

The second is an array containing one big string in the wrong format.

To turn your big string into an array, you'd need to use explode() to break it up on commas, and then trim() to remove the quotes from each item.

Thanks for the pointers Drew.

For anyone else, below is the final solution that worked for me. Some notes: - Trim wasn't effective for me so I just used a string replace function instead to strip out the quotes - It seemed more effective to remove the quotes before exploding

$accents = "'accents/spanish', 'accents/german', 'accents/welsh', 'accents/japanese', 'accents/german',";
$accents_trimmed = str_replace("'",'',$accents);
$accents_trimmed_array = explode(", ",$accents_trimmed);

perch_collection('Artists',
    array(
    'category' => $accents_trimmed_array,
    )
);
Drew McLellan

Drew McLellan 2638 points
Perch Support

Out of curiosity, how did you end up in this mess to begin with?

I'm in a similar situation to Alfonso here — https://forum.grabaperch.com/forum/04-19-2016-getting-multiple-values-for-the-same-parameter

I'm trying to break up a search query string so I can provide results for multiple categories.

The search string might be something like search.php?age=20s&age=2&accent=spanish&accent=german&accent=welsh&accent=japanese&accent=german

I'm sure I'm going about this in a completely convoluted way but PHP is not my core skill-area so I'm just happy to be getting somewhere :-)

Drew McLellan

Drew McLellan 2638 points
Perch Support

That certainly doesn't seem optimal. Good luck.

Thanks. I'll post the website to the Show and Tell when it's done—maybe the use case will be clearer then!