kath
kath

Reputation: 195

Getting values after foreach loops

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!

Upvotes: 0

Views: 82

Answers (3)

Girish
Girish

Reputation: 12117

Try this

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

Upvotes: 2

anik4e
anik4e

Reputation: 493

Check it:

$val = array(); 
foreach ($values as $valuesKey => $value) {     
   $val[] = $valuesKey;
}
$val = implode(",", $val);
do_shortcode('[playlist type="audio" ids="'.$val.'"][/playlist]');

Upvotes: 0

Amal Murali
Amal Murali

Reputation: 76646

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

Upvotes: 2

Related Questions