0

How can I include an string in an array?

emailconfig.php

$globalemail '[email protected]'=>'site'";

I want to make a new array like this:

sendemail.php

include "emailconfig.php"
$fulllist=array('[email protected]'=>'forum', '$globalemail');
// the Array MUST must appear above, ideally it would look like this
// $fulllist=array('[email protected]'=>'forum', '[email protected]'=>'site');

It brings PHP error because of the =>

6
  • So $globalemail contains the string "[email protected]'=>'site'"? (p.s. it's missing a ' before info) Commented Jan 31, 2012 at 21:47
  • 5
    -scratches head- This makes no sense at all. What exactly are you trying to do? Commented Jan 31, 2012 at 21:47
  • It's amazing how debilitating a failure to use the correct terminology can be to the communication process. Commented Jan 31, 2012 at 21:51
  • sorry guys, I updated my code, it cannot be anything else, the final line needs to be like that fulllist in the comments. Can't use trim etc Commented Jan 31, 2012 at 21:55
  • 1
    $globalemail '[email protected]'=>'site'"; is not valid PHP. What exactly does emailconfig.php contain? Commented Jan 31, 2012 at 21:55

3 Answers 3

2

One way is: in your emailconfig.php, you should have 2 variables, $globalemailkey and $globalemailvalue.

$globalemailkey = '[email protected]';
$globalemailvalue = 'site';

$fulllist = array('[email protected]'=>'forum', $globalemailkey => $globalemailvalue);

Or, store an array in emailconfig.php, and use array_merge.

$globalemail = array('[email protected]' => 'site');

$fulllist = array('[email protected]'=>'forum');
$fulllist = array_merge($fulllist, $globalemail);
Sign up to request clarification or add additional context in comments.

2 Comments

Hi thank you so much for this. I really love learning in PHP. I really enjoy it. What a great language. Beats Java any day.
PHP is fun :-) Glad I could help, happy coding :-D
2
$fulllist=array('[email protected]'=>'forum');
$globalemail = "[email protected]'=>'site'";
$parts = explode('=>', $globalemail);
$fulllist[trim($parts[0], "'")] = trim($parts[1], "'");

http://ideone.com/mmvu9

3 Comments

It has to look like the comment in my code. Sorry, I updated it just now.. apologies
@TheBlackBenzKid: yes, and it does look like you want. Check the ideone link
"repped" for the efforts. Went with the simple solution below.
2

You could But You Shouldn't use eval to do something like eval("array($yourstring)");. But you shouldn't. really. please.

You can do all sorts of things like preg-match or explode, but couldn't you easier find the source of those pieces of information, and work from there?

3 Comments

This seems to be an opinion? Thanks for the comments though! I went with Rocket solution!
Although it is an opinion, eval is widely frowned upon and I personally believe him to be right in saying that you shouldn't use it.
There are many drawbacks to using it. You can call it an opinion, but that doesn't change those drawbacks. To "back up" my claims, head over to the official documentation: php.net/manual/en/function.eval.php and check out the big yellow "caution" block, with the bold&italic notice Its use thus is discouraged..

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.