0

Hi all,

In PHP just wondering if anyone knows whether one method of creating multiple indexes within an array is more efficient / preferable to another?

`

$listing['one'] = $x;
$listing['two'] = $y;
$listing['three'] = $z;

`

or:

    $listing = [
                    ['one'] => $x,
                    ['two'] => $y,
                    ['three'] => $z
                ];

Most of the time I unthinkingly do the former, but it occured to me that it's quite possible the latter is more efficient.

Any thoughts?

3
Contributors
3
Replies
21
Views
1 Year
Discussion Span
Last Post by James_43
0

Hi James,

I think your second listing should actually be coded:-

$listing = [
    'one' => $x,
    'two' => $y,
    'three' => $z
];
0

So according to the test I just ran (and I'm completely unfamilair with memoery testing so for all I know my method was just pointless). But it seems there is definitely an improvement in using the latter method.

Calling the variable multiple times = 349616

Calling the variable once = 348600

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.