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;



    $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?

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