TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Question regarding array_push (http://www.talkphp.com/general/1442-question-regarding-array_push.html)

CMellor 11-13-2007 05:49 AM

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?

Haris 11-13-2007 07:05 AM

Quote:

Originally Posted by CMellor (Post 3997)
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'; .

Karl 11-13-2007 03:39 PM

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')); 


Wildhoney 11-13-2007 03:51 PM

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';

CMellor 11-13-2007 08:07 PM

Ah, cool, cheers lad's.

DragonBe 11-14-2007 10:50 AM

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...

Karl 11-14-2007 01:16 PM

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.

Wildhoney 11-14-2007 01:39 PM

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.)


All times are GMT. The time now is 12:20 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0