Array Problem

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
Mute
Forum Newbie
Posts: 15
Joined: Mon Jul 10, 2006 9:02 am

Array Problem

Post by Mute »

Hi,

Can someone tell me how to mimic the following using array_push?

Code: Select all

$replacements = array(
    "test" => array("!*EMAIL*!" => "email@email.com"),
    "dummy" => array("!*EMAIL*!" => "dude@dude.com"),
    "qwerty" => array("!*EMAIL*!" => "test@test.com")
  );  
Cheers!
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

You can't mimic the creation of an array with array_push(). array_push() simply adds elements to an array.

EDIT| I suppose you could create an empty array, and array push your 3 arrays into it.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Mute
Forum Newbie
Posts: 15
Joined: Mon Jul 10, 2006 9:02 am

Post by Mute »

That's what I'm trying to do. Basically, I've got a loop with all the details and need to push these into an array but can't seem to get it to work with the 2-d array.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Code: Select all

$replacements = array();

$test = array ('blabhalbhalba');

array_push($replacements, $test);
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Mute
Forum Newbie
Posts: 15
Joined: Mon Jul 10, 2006 9:02 am

Post by Mute »

I know how to do an array_push. I just can't do it with the 2-d array.

I'm trying this but it's not working.

Code: Select all

$replacements = array(); 
array_push($replacements, $test);
array_push($stack, "matt@advergamer.co.uk" => array("!*EMAIL*!" => "matt@advergamer.co.uk"));
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Mute wrote:I know how to do an array_push. I just can't do it with the 2-d array.

I'm trying this but it's not working.

Code: Select all

$replacements = array(); 
array_push($replacements, $test);
array_push($stack, "matt@advergamer.co.uk" => array("!*EMAIL*!" => "matt@advergamer.co.uk"));
Where's $stack coming from?
the second argument in the second array push is not a valid argument. it's a string and an array. wrap it in array()
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Mute
Forum Newbie
Posts: 15
Joined: Mon Jul 10, 2006 9:02 am

Post by Mute »

scottayy wrote:
Mute wrote:I know how to do an array_push. I just can't do it with the 2-d array.

I'm trying this but it's not working.

Code: Select all

$replacements = array(); 
array_push($replacements, $test);
array_push($stack, "matt@advergamer.co.uk" => array("!*EMAIL*!" => "matt@advergamer.co.uk"));
Where's $stack coming from?
the second argument in the second array push is not a valid argument. it's a string and an array. wrap it in array()
Sorry, long day and my last post made no sense. Let me start again (and thanks for the help).

Code: Select all

$replacements = array(); 
array_push($replacements , "matt@advergamer.co.uk" => array("!*EMAIL*!" => "matt@advergamer.co.uk"));
That's what I'm trying and it's not working. You say that I need to wrap the whole statement in array(). Like thus?

Code: Select all

$replacements = array(); 
array_push($replacements , array("matt@advergamer.co.uk" => array("!*EMAIL*!" => "matt@advergamer.co.uk")));
That works. Kind of but it's not quite right. If I print out the array I get....

Code: Select all

Array ( [0] => Array ( [matt@advergamer.co.uk] => Array ( [!*EMAIL*!] => matt@advergamer.co.uk ) ) )
I need to have....

Code: Select all

Array ( [matt@advergamer.co.uk] => Array ( [!*EMAIL*!] => matt@advergamer.co.uk ) )
Thanks once again mate.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Ah. I guess I didn't understand your question fully. Apologies.

Code: Select all

$replacements['matt@advergamer.co.uk'] = array('!*EMAIL*!" => "matt@advergamer.co.uk");
Should do the trick.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Mute
Forum Newbie
Posts: 15
Joined: Mon Jul 10, 2006 9:02 am

Post by Mute »

Beautiful! We got there eventually.

Looking at that I'm an idiot, problem was so simple but I was trying to complicate it by using array_push.

Thanks buddy, life saver!
Post Reply