0

I'm trying to implode variables, but it's not working correctly:

$models = array("$model0, $model1");

$modelfinal = implode("," , $models);

$modelfinal only returns , ,

I'm guessing I'm way off...anybody?

8
  • 4
    Is $models meant to have only one element, or is that a typo? Commented Aug 7, 2013 at 23:44
  • I'm not sure I follow, I'm trying to implode $model0 and $model1 Commented Aug 7, 2013 at 23:45
  • Ignore my answer i was thinking of explode my bad :P Commented Aug 7, 2013 at 23:46
  • @Ken: You already have them concatenated: "$model0, $model1" Shouldn’t take anything more. Commented Aug 7, 2013 at 23:46
  • @Ken what is $model0 and $model1, what does var_dump($model0, $model1) returns? Commented Aug 7, 2013 at 23:47

2 Answers 2

4

The following statement creates an array with exactly one string in it, which is comprised of the values of two (apparently) undefined variables separated with a comma:

$models = array("$model0, $model1");

The end result is the same as if you had done this:

$models = array(", ");

Now you're imploding it using a comma as the separator, which doesn't do anything since there's only one element in the array (a string with a comma and a space).

Assuming $model0 and $model1 are defined (which is a problem you'll need to look into first), you can get your desired result either by:

  • directly using $modelfinal = "$model0, $model1",
  • or by using $models = array($model0, $model1); followed by the implode.
Sign up to request clarification or add additional context in comments.

Comments

2

here is your problem "$model0, $model1" change it to this code

$models = array($model0,$model1);

5 Comments

Why not array($model0, $model1)? Yuck.
I just copy from question, i fix it now
Not me, but I think it's because this isn't actually his problem (it's a problem, but he's got other problems going on)
@Prix his output was , ,, which obviously means there's an issue with $model0 and $model1 as well as this
@Dave i think he is using array for two element not one. is is very strange to convert a string to array , then implode that to string again !!

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.