jarvis Posted October 12, 2016 Share Posted October 12, 2016 Hi All, I think I'm being pretty daft! I currently have the following: $categories = array( 'cat 1', 'cat 2', 'cat 3'); This is fine but the issue is it required someone manually entering the values. I therefore setup a loop which gets all my categories: $subcats = array(); foreach($wctTerms as $wctTerm) { $subcats[] = $wctTerm->slug; } I can then use: $comma_separated = implode (", ", $subcats); Which spits out what I need: cat 1, cat 2, cat 3 etc. But how do I get this back into the original $categories part. I thought: $categories = array( $comma_separated ); But that doesn't work Feeling pretty daft right now! Any pointers much appreciated Quote Link to comment https://forums.phpfreaks.com/topic/302321-a-variable-as-array-value/ Share on other sites More sharing options...
cyberRobot Posted October 12, 2016 Share Posted October 12, 2016 I think you're looking for explode(). More information can be found here: http://php.net/manual/en/function.explode.php Quote Link to comment https://forums.phpfreaks.com/topic/302321-a-variable-as-array-value/#findComment-1538219 Share on other sites More sharing options...
Barand Posted October 12, 2016 Share Posted October 12, 2016 So, if I understand you, there is an array ($subcats) which you implode into a comma separated string and which you now want to put back into an array. But you already have that array - $subcats 1 Quote Link to comment https://forums.phpfreaks.com/topic/302321-a-variable-as-array-value/#findComment-1538220 Share on other sites More sharing options...
ginerjm Posted October 12, 2016 Share Posted October 12, 2016 How about: $categories = $wctTerms; Quote Link to comment https://forums.phpfreaks.com/topic/302321-a-variable-as-array-value/#findComment-1538221 Share on other sites More sharing options...
Solution jarvis Posted October 12, 2016 Author Solution Share Posted October 12, 2016 (edited) Thank you all for you comments @Barand, I think that was the missing piece I needed. So if I've understood you correctly, I could just do this: $categories = array(); foreach($wctTerms as $wctTerm) { $categories[] = $wctTerm->slug; } Would that work? I think I've over complicated something very simple (the usual!) Edited October 12, 2016 by jarvis Quote Link to comment https://forums.phpfreaks.com/topic/302321-a-variable-as-array-value/#findComment-1538224 Share on other sites More sharing options...
cyberRobot Posted October 12, 2016 Share Posted October 12, 2016 That should work just fine. How often do you think you will need the imploded version of the categories in a single script? Depending on how often you need the value, you can just leave the categories as an array. Then just implode the values as needed. Quote Link to comment https://forums.phpfreaks.com/topic/302321-a-variable-as-array-value/#findComment-1538225 Share on other sites More sharing options...
ginerjm Posted October 12, 2016 Share Posted October 12, 2016 I wish I knew what 'slug' was.... Quote Link to comment https://forums.phpfreaks.com/topic/302321-a-variable-as-array-value/#findComment-1538226 Share on other sites More sharing options...
jarvis Posted October 12, 2016 Author Share Posted October 12, 2016 @ginerjm - its a wordpress/woocommerce term @cyberRobot - if I go the route as per my code a minute ago, shouldnt this negate the need to use implode? Quote Link to comment https://forums.phpfreaks.com/topic/302321-a-variable-as-array-value/#findComment-1538227 Share on other sites More sharing options...
cyberRobot Posted October 12, 2016 Share Posted October 12, 2016 I wish I knew what 'slug' was.... I have a feeling it's related to WordPress. The "wtc" part seems like it's connected to the Woocommerce plugin. More information about WordPress slugs can be found here: https://codex.wordpress.org/Glossary#Slug Quote Link to comment https://forums.phpfreaks.com/topic/302321-a-variable-as-array-value/#findComment-1538228 Share on other sites More sharing options...
cyberRobot Posted October 12, 2016 Share Posted October 12, 2016 (edited) @cyberRobot - if I go the route as per my code a minute ago, shouldnt this negate the need to use implode? That depends on what you are looking to do. If you don't need the categories/slugs as a string, you don't need to use implode. Edited October 12, 2016 by cyberRobot Quote Link to comment https://forums.phpfreaks.com/topic/302321-a-variable-as-array-value/#findComment-1538229 Share on other sites More sharing options...
Barand Posted October 12, 2016 Share Posted October 12, 2016 I wish I knew what 'slug' was.... A homeless snail ? 1 Quote Link to comment https://forums.phpfreaks.com/topic/302321-a-variable-as-array-value/#findComment-1538230 Share on other sites More sharing options...
jarvis Posted October 12, 2016 Author Share Posted October 12, 2016 I guess if the code above means i don't need to convert to a string, then put the string back into the array then I think we're all good apologies if I've confused the matter All I want to do is automate the process so instead of typing everything into $categories = array ('manually adding each category'); I thought if I loop the categories I can make this automatically happen I've a feeling I got myself in a muddle and therefore, if I set up my loop and add each value into the $categories array, this will do the same as the line of code above? Quote Link to comment https://forums.phpfreaks.com/topic/302321-a-variable-as-array-value/#findComment-1538231 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.