We help IT Professionals succeed at work.

Why is my implode function empty

peter-cooper
peter-cooper asked
on
132 Views
Last Modified: 2017-04-25
Hello
I have created a session that after doing 'print_r($_SESSION['boxdest2']);' shows these values: 'DEMO9992,DEMO9963'. The problem is that in the code I am using, it is empty. Obviously my code is wrong and would be grateful if someone could point out my error. thanks

$boxes = implode(',', $_SESSION['boxdest2']);

Open in new window

Comment
Watch Question

Marco GasiFreelancer
CERTIFIED EXPERT
Top Expert 2010

Commented:
implode() function takes an array and converts it to a string using given separator. Are you sure your session 'boxdest2' variable be an array? Instead of using print_r() you could use var_dump():
echo "<pre>";
var_dump($_SESSION['boxdest2'];
echo "</pre>";

Open in new window

Author

Commented:
@narco this is the result of var_dump. Thanks

string(22) "DEMO932,DEMO93,DEMO929"

Open in new window

Author

Commented:
This works but I need to lose the echo and create a variable that I can use elsewhere on page. Thanks

$x = explode(',', $_SESSION['boxdest2']);
      foreach ($x as $value)
            {
                  $boxes = "( " . $value . " )" . ' ';
                    echo $boxes;
              }
Most Valuable Expert 2011
Author of the Year 2014

Commented:
We can see what you have in $_SESSION['boxdest2'], so please tell us what do you want to have in $boxes?  We don't need to see code that doesn't work (I already have plenty of that in my own collection).  We just need to see what you have and what you want.  Then we can provide the code to bridge the gap.

Author

Commented:
@Ray Very simply, the code I have posted in my latest comment works when i echo results. However, i need to lose the echo and have a variable that I can use elsewhere in page. Thanks
Freelancer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Most Valuable Expert 2011
Author of the Year 2014

Commented:
I'm with Marco -- I don't get the objective either.

$_SESSION['boxdest2'] contains the information you need, right?  Why not just use that variable?  Why create another variable?  If we understand your use case, and the format you want for the new variable, it should be easy to help.

Author

Commented:
Thanks Marco

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.