TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-13-2007, 05:49 AM   #1 (permalink)
The Acquainted
Upcoming Programmer 
 
CMellor's Avatar
 
Join Date: Sep 2007
Location: Leeds, UK
Posts: 141
Thanks: 6
CMellor is on a distinguished road
Default Question regarding array_push

Hey, was fooling around with some array functions, and just wondered...

Ya know how array_push adds a new value to your array? Can it also add a key, because I tried:

PHP Code:
array_push($array'Chris' => 'Mellor'); 
but got an error.

I'm sure it's possible, anybody an idea?
__________________
Not quite a n00b...
CMellor is offline  
Reply With Quote
Old 11-13-2007, 07:05 AM   #2 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

Quote:
Originally Posted by CMellor View Post
Hey, was fooling around with some array functions, and just wondered...

Ya know how array_push adds a new value to your array? Can it also add a key, because I tried:

PHP Code:
array_push($array'Chris' => 'Mellor'); 
but got an error.

I'm sure it's possible, anybody an idea?
http://php.net/array_push

Quote:
Note: If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.
You can use $aArray['Chris'] = 'Mellor'; .
Haris is offline  
Reply With Quote
Old 11-13-2007, 03:39 PM   #3 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

you forgot to wrap the key/value pair in an array constructor call, the correct way to achieve this with array_push is:

PHP Code:
array_push($array, array('Chris' => 'Mellor')); 
* Edit, got my self in a muddle here, this is an example of array_merge, not array push:

PHP Code:
array_merge($array, array('Chris' => 'Mellor')); 
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

Last edited by Karl : 11-14-2007 at 01:14 PM.
Karl is offline  
Reply With Quote
Old 11-13-2007, 03:51 PM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

You'd be well advised to use Haris' method as otherwise you'll be calling a function which can quite easily be added using standard PHP. Therefore:

php Code:
$aMyArray = array('Blue' => 'Ocean', 'Green' => 'Grass');
$aMyArray['Yellow'] = 'Sunshine';
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-13-2007, 08:07 PM   #5 (permalink)
The Acquainted
Upcoming Programmer 
 
CMellor's Avatar
 
Join Date: Sep 2007
Location: Leeds, UK
Posts: 141
Thanks: 6
CMellor is on a distinguished road
Default

Ah, cool, cheers lad's.
__________________
Not quite a n00b...
CMellor is offline  
Reply With Quote
Old 11-14-2007, 10:50 AM   #6 (permalink)
The Wanderer
PHP Guru Advanced Programmer Zend Certified 
 
DragonBe's Avatar
 
Join Date: Nov 2007
Location: according to my wife: on the Net
Posts: 19
Thanks: 0
DragonBe is on a distinguished road
Default best-practice: $array[] = $item or array_push($array, $item)?

Since we talk about array_push here, I might as well ask the question here. What's better or faster ?

Code:
foreach ($complex_array as $key => $item) {
   $array[] = $item;
}
or

Code:
foreach ($complex_array as $key => $item) {
   array_push($array, $item);
}
Just a wondering of me...
Send a message via ICQ to DragonBe Send a message via Skype™ to DragonBe
DragonBe is offline  
Reply With Quote
Old 11-14-2007, 01:16 PM   #7 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

You've gotta assume it's going to be the non-function approach which is faster. In fact, I'd bet my grandmother on it. As for better, I guess that's down to personal preference.
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
Old 11-14-2007, 01:39 PM   #8 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

I've just done a little test and, well, let the numbers speak for themselves:

Quote:
$aArray[] = 'Test';
This took this many seconds: 1.18
This took this many seconds: 1.20
This took this many seconds: 1.16

array_push($aArray, 'Test');
This took this many seconds: 1.86
This took this many seconds: 1.90
This took this many seconds: 2.01
(Each one was looped a million times - had to increase the PHP memory limit, of course :) It wasn't having any of it - trying to squeeze it into an 8MB buffer.)
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 09:13 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design