So, I started using Zend Studio and it gives me warnings on my foreach loops when I only need to use the $key and not the $value.

In other words:
foreach($array as $key => $value)
{
do_something_with($key);
}
The above will throw a warning because I am not using $value.

I lived with the warnings for a while, until I decided to try this.
foreach($array as $key => $key)
{
do_something_with($key);
}

This seems to work and puts the "key information" in the $key variable (just fine). Also Zend doesn't throw the warning anymore (YEAH). Anyways my question is:

Does anyone know any sort of problems which will arise from doing this?

I have never tried it before and I haven't found any sort of information on doing this. I am using "PHP Version 5.2.4-2ubuntu5.4" and I am specifically interested if this causes errors (or perhaps puts the "value information" in the $key variable) in other versions of PHP such as 4 or 6.

Thanks for any help you can give.

    zend is just trying to be helpful telling you of a var you defined and did not use, its not a php issue but a zend error checking one, its perfectly valid to do

    foreach($array as $key => $value)
    {
    do_something_with($key);
    }

    and never use the $value

    i guess if you really care you make a new array using array_keys() and foreach on that

      You don't have to store the new array either:

      foreach(array_keys($array) as $key)
          do_something_with($key);

        Yah, I know I could use array_key I just figured it was a waisted step and didn't really want to.

        I don't really care about the warning which Zend gives me, I just thought it would be nice to code it in such away that it doesn't throw a Warning. I think I would rather have the warning then use array_key.

        What I really want to know is if there are any problems with doing it the new way? Is the behavior going to change in newer or older versions of PHP? (Basically is it going to start populating $key with "value information"?)

        Is what I am doing reliable? Did the php people design it that way? Or am I just exploiting some sort of glitch which might change with time?

        This type of information is what I was looking for. Not alternative ways of doing the same thing.

          amcnea wrote:

          I think I would rather have the warning then use array_key.

          Any particular reason why?

          amcnea wrote:

          Is the behavior going to change in newer or older versions of PHP? (Basically is it going to start populating $key with "value information"?)

          Well, if PHP developers decided that a function called array_key should suddenly return array values, then I sure hope it's something released on April 1st, otherwise I would be quite perplexed.

          As for the rest of your post, I suppose the reason you're getting a warning is that Zend just wanted to make sure you realized that the whole point for PHP to loop through the array was (normally) to store each value into a variable for you to use. Instead, you're just interested in the keys and are just throwing the values away.

          I'm not sure which would be more memory-efficient; using a foreach($key=>value) statement and just ignoring $value versus a foreach(array_keys()) statement. I'd be interested in the results, but to me it would make more sense to see a foreach(array_keys()) statement; that why I'd know that the loop was specifically for looking up the array keys.

            amcnea;10896253 wrote:

            What I really want to know is if there are any problems with doing it the new way? Is the behavior going to change in newer or older versions of PHP? (Basically is it going to start populating $key with "value information"?)

            yes! and all variables named cat will be replaced with pictures of a cute pussy!

              i guess for changing older versions of php you expect them to use that time machine brought on ebay?

              ----ok i'll shut up now :-)

                Apparently there is some confusion as to what I am talking about.

                It has to do with this:

                foreach($array as $key => $value)
                {
                do_something_with($key);
                }
                

                Versus this:

                foreach($array as $key => $key)
                {
                do_something_with($key);
                }
                

                My post has ABSOLUTELY NOTHING to do with the function array_key.

                I am wondering if the code below is valid php code.

                foreach($array as $key => $key)
                {
                do_something_with($key);
                }
                

                The reason I don't want to use array_key is because it is:
                1) slower (I checked)
                2) less memory efficient (I checked)
                3) More code

                Again I will state my questions with regards to the following code:

                foreach($array as $key => $key)
                {
                do_something_with($key);
                }
                

                1) Is this valid PHP code?
                2) Is the behavior going to change in newer or older versions of PHP? (Basically is it going to start populating $key with "value information"?)
                3) Is what I am doing reliable?
                4) Did the php people design it that way? Or am I just exploiting some sort of glitch which might change with time?

                  If by "valid" you mean will it parse, yes, it's perfectly valid. I'm not sure if you can rely on it or not, as it seems like an odd way of doing it.

                  EDIT: Dagon posted before I did; the PHP bug report is the reason why I wouldn't use that method.

                    Thanks for the bug report.

                    In case you are interested I checked it and it doesn't seem to mess up my array using php 5 as it does in php 4.

                    But it seems unpredictable and I found some other bug reports asking them to make php through an error or warning when you do it that way.

                    So yah, definitely looks like the behavior changes in php4 and overrides the array. I think this is something I should stay away from.

                    Thanks.

                      Write a Reply...