Jump to content

A variable as array value


Go to solution Solved by jarvis,

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/302321-a-variable-as-array-value/
Share on other sites

  • Solution

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 by jarvis

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.

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

@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 by cyberRobot

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.