Question

I have a foreach loop that gives me a number of values after looping through an array, like so:

842,844,841,839,838

This is the loop:

foreach ($values as $valuesKey => $value) {     
    echo $valuesKey . ',';
}

I need to work with those values after the loop finishes, how can I go about it? I want to put the list of values into another function. Is this even possible??

do_shortcode('[playlist type="audio" ids="/* values should go here */"][/playlist]');

It's supposed to look like this if it works:

do_shortcode('[playlist type="audio" ids="842,844,841,839,838"][/playlist]');

Thanks for anyone who can point me into the right direction!

Was it helpful?

Solution

There's no need for a loop. You can use implode() to join the array keys into a comma-separated string:

do_shortcode('[playlist type="audio" ids="'.implode(',', array_keys($values)).'"][/playlist]');

OTHER TIPS

Try this

implode(",", array_keys($values))

Check it:

$val = array(); 
foreach ($values as $valuesKey => $value) {     
   $val[] = $valuesKey;
}
$val = implode(",", $val);
do_shortcode('[playlist type="audio" ids="'.$val.'"][/playlist]');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top