Fastest way to iterate array in PHP

后端 未结 3 804
刺人心
刺人心 2020-12-09 09:08

I\'m studying for the Zend PHP certification.

Not sure the answer to this question.

Question: What is the best way to iterate and mod

相关标签:
3条回答
  • 2020-12-09 09:24

    You can iterate and modify every element of an array with any of the shown constructs. But some notes on that:

    b) Is only useful if the array is a numeric array with the keys from 0 to n-1.

    c) Is useful for both kinds of arrays. Additionally $value is a reference of the element’s value. So changing $value inside foreach will also change the original value.

    d) Like c) except $value is a copy of the value (note that foreach operates on a copy of $array). But with the key of the element you can access and change the original value with $array[$key].

    e) Like d). Use $array[$key] to access and change the original element.

    0 讨论(0)
  • 2020-12-09 09:28

    SPL would be the best answer here.

    0 讨论(0)
  • 2020-12-09 09:46

    From these options C would be the obvious answer.

    The remaining options (besides A) may be used to achieve that, depending on the code inside the parenthesis, but the question does NOT show that code. So it must be C.

    And you are answering the wrong question - yes doing count() before the for cycle will improve performance, but this question is not about performance.

    0 讨论(0)
提交回复
热议问题