0

I have a script which can be used multiple times per script, each instance of the script requires some php stuff to be done, I need to create a unique name for each instance variable e.g.

I need:

$typedbefore1 = get_post_meta( $text, '_cmb2_typed_text', true );
$typed1 = '"' . implode('","', $typedbefore1) . '"';

$typedbefore2 = get_post_meta( $text, '_cmb2_typed_text', true );
$typed2 = '"' . implode('","', $typedbefore2) . '"';

$typedbefore3 = get_post_meta( $text, '_cmb2_typed_text', true );
$typed3 = '"' . implode('","', $typedbefore3) . '"';

The variable $text is a number generated by the user so I can use that e.g.

$typedbefore.$text = get_post_meta( $text, '_cmb2_typed_text', true );
$typed.$text = '"' . implode('","', $typedbefore.$text) . '"';

That doesn't work (obviously), is there a way to do what I need?

Converted to arrays but my implode isn't working:

$typedbefore = array();
$typedbefore[$text] = get_post_meta( $text, '_cmb2_typed_text', true );
$typed = array();
$typed[$text] = '"' . implode('","', $typedbefore[$text]) . '"';

It's still storing the $typed as an array, how can I do the second part so it implodes the data from $typedbefore?

1
  • 2
    Why not make array for the values? Commented Dec 19, 2014 at 14:00

2 Answers 2

2

You really should use arrays, as MightyPork suggested...:

$typedbefore = array();
$typedbefore[$text] = "value";

UPDATE: To answer OP last question: from here I see wordpress finction get_post_meta() returns array only if it's last parameter is false. But I see you use true, so it should return a single value. So you shouldn't use implode (which expects an array as 2nd parameter) with a single value. I don't really understand what's your definitive goal, so I can't give a more specific comment, sorry...

Sign up to request clarification or add additional context in comments.

2 Comments

Added more to my question, it has helped with the text part but I can't get the data from $typedbefore to be imploded, as it's caught in 2 arrays
Basically the post meta is an array of text strings which I am then printing into a peice of javascript which needs to be comma'd hence the implode.
1

You can use variable variables like:

$varname = "typedbefore".$text;
echo $$varname;

This is the same, if you say:

echo $typedbefore1 //If $text == 1

See here.

But much more better, if your are collecting your data into an array, and use that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.