![]() |
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:
I'm sure it's possible, anybody an idea? |
Quote:
Quote:
$aArray['Chris'] = 'Mellor'; . |
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:
PHP Code:
|
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:
|
Ah, cool, cheers lad's.
|
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) {Code:
foreach ($complex_array as $key => $item) { |
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.
|
I've just done a little test and, well, let the numbers speak for themselves:
Quote:
|
| 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