Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly loop through array keys in PHP [closed]

If I have an array with multiple keys and values, like this.

$array = array(
  'key1' => 'value1',
  'key2' => 'value2',
);

Are there any standards how to loop through an array when I only use the keys? Solution 1:

foreach(array_keys($array) as $array_key) {
  echo $array_key;
}

Solution 2:

foreach($array as $array_key => $array_value) {
  echo $array_key;
}

I can see the advantage in solution 1 as it doesn't call the unused variable $array_value. Is there a difference in performace between these two? Are there any standards how to do it?

like image 939
Raphael Portmann Avatar asked Nov 16 '17 16:11

Raphael Portmann


People also ask

How do you iterate through an associative array in PHP?

The foreach() method is used to loop through the elements in an indexed or associative array. It can also be used to iterate over objects. This allows you to run blocks of code for each element.

How do you repeat a loop in PHP?

do...while - loops through a block of code once, and then repeats the loop as long as the specified condition is true. for - loops through a block of code a specified number of times. foreach - loops through a block of code for each element in an array.

How do you use key value in foreach?

The foreach looping is the best way to access each key/value pair from an array. array_expr is an array. In every loop the value of the current element of the array is assigned to $value and the internal array pointer is advanced by one and the process continue to reach the last array element. array_expr is an array.


1 Answers

From my quick and dirty benchmark I did the following 50 times:

Solution 1 (worst):

foreach (array_keys($array) as $array_key) {
  echo $array_key;
}

Size of array: 1000000

  • Min: 0.11363291740417
  • Avg: 0.11681462287903
  • Max: 0.14569497108459

Size of array: 9999999

  • Min: 1.3098199367523
  • Avg: 1.3250577354431
  • Max: 1.3673560619354

Solution 2 (middle):

foreach ($array as $array_key => $array_value) {
  echo $array_key;
}

Size of array: 1000000

  • Min: 0.10167503356934
  • Avg: 0.10356130123138
  • Max: 0.11027193069458

Size of array: 9999999

  • Min: 1.2077870368958
  • Avg: 1.2256314325333
  • Max: 1.2829539775848

Solution 3 (best):

$array_keys = array_keys($array);
foreach ($array_keys as $array_key) {
  echo $array_key;
}

Size of array: 1000000

  • Min: 0.090911865234375
  • Avg: 0.092938437461853
  • Max: 0.097810983657837

Size of array: 9999999

  • Min: 1.0793349742889
  • Avg: 1.0941110134125
  • Max: 1.1402878761292

So you can see solution 3 is the quickest option if you only want to look through array keys :)

Hope this helps.


Code:

<?php
class DirtyBenchmarker {
  private $results = [];
  private $size_of_array;

  public function __construct($size_of_array)
  {
    $this->size_of_array = $size_of_array;
    echo 'Size of array: ' .  $this->size_of_array . PHP_EOL;
  }

  private function solution1() {
    $array = range(0, $this->size_of_array - 1);
    ob_start();
    $start = microtime(true);
    foreach (array_keys($array) as $array_key) {
      echo $array_key;
    }
    $finish = microtime(true) - $start;
    $echod = ob_get_clean();
    $this->results['solution1'][] = $finish;
  }

  private function solution2() {
    $array = range(0, $this->size_of_array - 1);
    ob_start();
    $start = microtime(true);
    foreach ($array as $array_key => $array_value) {
      echo $array_key;
    }
    $finish = microtime(true) - $start;
    $echod = ob_get_clean();
    $this->results['solution2'][] = $finish;
  }

  private function solution3() {
    $array = range(0, $this->size_of_array - 1);
    $array_keys = array_keys($array);
    ob_start();
    $start = microtime(true);
    foreach ($array_keys as $array_key) {
      echo $array_key;
    }
    $finish = microtime(true) - $start;
    $echod = ob_get_clean();
    $this->results['solution3'][] = $finish;
  }

  public function benchmark() {
    $this->solution1();
    $this->solution2();
    $this->solution3();
  }

  public function getResults()
  {
    echo PHP_EOL . 'Solution 1:' . PHP_EOL;
    echo 'Min: ' . min($this->results['solution1']) . PHP_EOL;
    echo 'Avg: ' . array_sum($this->results['solution1']) / count($this->results['solution1']) . PHP_EOL;
    echo 'Max: ' . max($this->results['solution1']) . PHP_EOL;

    echo PHP_EOL . 'Solution 2:' . PHP_EOL;
    echo 'Min: ' . min($this->results['solution2']) . PHP_EOL;
    echo 'Avg: ' . array_sum($this->results['solution2']) / count($this->results['solution2']) . PHP_EOL;
    echo 'Max: ' . max($this->results['solution2']) . PHP_EOL;

    echo PHP_EOL . 'Solution 3:' . PHP_EOL;
    echo 'Min: ' . min($this->results['solution3']) . PHP_EOL;
    echo 'Avg: ' . array_sum($this->results['solution3']) / count($this->results['solution3']) . PHP_EOL;
    echo 'Max: ' . max($this->results['solution3']) . PHP_EOL;
  }
}

$benchmarker = new DirtyBenchmarker(1000000);
$runs = 50;
for ($i = 0; $i < $runs; $i++) {
  $benchmarker->benchmark();
}
$benchmarker->getResults();

$benchmarker = new DirtyBenchmarker(9999999);
$runs = 50;
for ($i = 0; $i < $runs; $i++) {
  $benchmarker->benchmark();
}
$benchmarker->getResults();
like image 143
alistaircol Avatar answered Oct 05 '22 00:10

alistaircol