Array syntax...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Josh1billion
Forum Contributor
Posts: 316
Joined: Tue Sep 11, 2007 3:25 pm

Array syntax...

Post by Josh1billion »

I'm using SwiftMailer and trying to use the Decorator plugin.

The code shown there goes like this:

Code: Select all

 
$replacements = array(
    "joe@bloggs.com" => array("{name}" => "Joe", "{weather}" => "chilly"),
    "fred@perry.com" => array("{name}" => "Fred", "{weather}" => "muggy")
);
 
I'm trying to do something like that, but I need to split it up into different code pieces (i.e. add an element to the array later in a while loop).

So I'm trying something like this, but it's not working:

Code: Select all

 
$replacements = array(
    "joe@bloggs.com" => array("{name}" => "Joe", "{weather}" => "chilly")
);
 
// .....................
 
$replacements[] = "fred@perry.com" => array("{name}" => "Fred", "{weather}" => "muggy");
 
Of course, the last line is incorrect and throws up a syntax/parse error. What's the correct syntax for that last line?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Array syntax...

Post by Christopher »

Code: Select all

$replacements[] = array("fred@perry.com" => array("{name}" => "Fred", "{weather}" => "muggy");
(#10850)
User avatar
Josh1billion
Forum Contributor
Posts: 316
Joined: Tue Sep 11, 2007 3:25 pm

Re: Array syntax...

Post by Josh1billion »

Did not work.. I think I tried that before at some point in time (when I was trying a bunch of combo's to try to get it to work).

Here's the exact code of what I have now, based upon the code you posted, it "compiles" fine, but Swift Mailer's Decorator plugin (linked to in first post) isn't reading it properly so the array format must be wrong....

Code: Select all

 
    $replacements = array(
        "null" => array("[USERNAME]" => "null", "[LEVEL]" => "1", "[CLASS]" => "Warrior")); // just putting in a basic entry to start things off
 
// ..............
 
 
 
// real data is added here.. the variables are correct
        $replacements[] = array($current['username'] => array("[USERNAME]" => $current['username'], "[LEVEL]" => $current['level'], "[CLASS]" => $current['class']));
 
User avatar
Josh1billion
Forum Contributor
Posts: 316
Joined: Tue Sep 11, 2007 3:25 pm

Re: Array syntax...

Post by Josh1billion »

Ah, probably because I'm using batchSend instead of Send... hold up, trying to fix it...
EDIT: Nevermind, apparently it IS supposed to work with batchSend() too... still don't know what the problem is, then.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Array syntax...

Post by John Cartwright »

batchSend() is only a wrapper of regular send() with some magic error handling fluff, nothing more. It shouldn't have an affect on the decorator plugin.

However, Swift expects the users email to be the key of the array.. so what I don't understand is why you are adding another dimension to the array.

Take a look at http://swiftmailer.org/wikidocs/v3/plugins/decorator, you should be doing something like

Code: Select all

 
$replacements[$row['email']] = array("[USERNAME]" => $current['username'], "[LEVEL]" => $current['level'], "[CLASS]" => $current['class']);
User avatar
Josh1billion
Forum Contributor
Posts: 316
Joined: Tue Sep 11, 2007 3:25 pm

Re: Array syntax...

Post by Josh1billion »

Ahh.. the e-mail is the key of the array, that explains it. :) Thanks JCart, it's working now. :)

(I haven't worked with arrays in PHP much to be honest, I didn't even know that you could have non-numerical keys for arrays :P)
Post Reply