11

When searching through an array,because I also program using Java,i always end up putting for(x=0;x<array.length ;x++){}
Now in php I have to deal with this

Comments
  • 4
    Why build your own loop? Why not use `forEach`?
  • 1
    Like @k0pernikus said, why do you not use a foreach. Commenting because I'm curious if there is a reason to not use foreach.
  • 5
    @aronmik there is one... Let's say you have two arrays you know they'll be the same length for each doesn't allow you to do two things at once ...

    I.e
    For (int I = 0; I < 5;I++) {
    Array0[I];
    Array1[I];
    }
    You can technically do it In foreach but then you'll be creating your own for loop
  • 2
    @FitzSuperUser thats still possible if those arrays are not key/value arrays. Just use foreach($list as $index => $item) and use index to do the things you want to do to the other array
  • 1
    @ruuds96 That is very logical, but some people (including me) just prefer C style stuff.
  • 2
    Be aware that putting the length of the array (or count for php) in a variable slightly improves performance.
  • 0
    @zmzmuazzam98 I get your hustle bro. I wprk with both the languages too 😅😅😅
  • 1
    Just use forEach
  • 0
    If you're "searching" for something in an array, maybe you don't even have to loop. array_search( ), anyone?
  • 0
    Btw array in java with foreach: for(type a : arr){System.out.println(a);}
Add Comment