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?

Recommended Answers

All 3 Replies

Hi James,

I think your second listing should actually be coded:-

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.