Jump to content

A variable as array value


jarvis
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
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
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.